Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

z-axis formatting in mplot3d

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?

like image 485
Magnus Avatar asked Jul 06 '11 12:07

Magnus


1 Answers

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)
like image 95
John Lyon Avatar answered Oct 23 '22 15:10

John Lyon