12

I am trying to make a surface plot in matplotlib, but I am unable to make the formatting of the z-axis look good. I want it to be on scientific notation, with the prefix along the axis and the exponent above the axis (like what you usually get in matlab). At the moment I can only get the values without scientific notation (with a comma and five zeros in front), or I get the prefixes but not the exponent...

Try 1: (gives scientific notation, but not the exponent)

from matplotlib import ticker

ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=3, cstride=3)
formatter = ticker.ScalarFormatter()
formatter.set_scientific(True)
formatter.set_powerlimits((-2,2))
ax.w_zaxis.set_major_formatter(formatter)

Try 2: (gives the values on decimal form, same as default output)

from matplotlib import ticker

ax = fig.gca(projection='3d')
ax.plot_surface(X, Y, Z, rstride=3, cstride=3)
ax.ticklabel_format(axis="z", style="sci", scilimits=(0,0))

What am I doing wrong here?

Magnus
  • 141
  • 2
  • 6
  • 1
    Which version of matplotlib are you using? You might be running into a bug that wasn't fixed until 1.0.1. http://old.nabble.com/Displaying-offsets-in-3d-plots-td30474615.html – Stephen Terry Jul 06 '11 at 16:39
  • 1
    @Stephen Terry: I am running 1.0.1. `Python 2.7.1 |EPD 7.0-2 (32-bit)| (r271:86832, Dec 3 2010, 15:41:32) [GCC 4.0.1 (Apple Inc. build 5488)] on darwin >>> import matplotlib >>> matplotlib.__version__ '1.0.1'` – Magnus Jul 07 '11 at 16:40

1 Answers1

4

When you create your ScalarFormatter, try the "use Math Text" parameter:

from matplotlib import ticker
niceMathTextForm = ticker.ScalarFormatter(useMathText=True)
ax.w_zaxis.set_major_formatter(niceMathTextForm)
John Lyon
  • 11,180
  • 4
  • 36
  • 44