Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent numbers being changed to exponential form in Python matplotlib figure

I'm using Matplotlib in Python to plot simple x-y datasets. This produces nice-looking graphs, although when I "zoom in" too close on various sections of the plotted graph using the Figure View (which appears when you execute plt.show() ), the x-axis values change from standard number form (1050, 1060, 1070 etc.) to scientific form with exponential notation (e.g. 1, 1.5, 2.0 with the x-axis label given as +1.057e3).

I'd prefer my figures to retain the simple numbering of the axis, rather than using exponential form. Is there a way I can force Matplotlib to do this?

like image 983
IanRoberts Avatar asked Feb 05 '13 16:02

IanRoberts


People also ask

How do I stop Matplotlib from scientific notation?

To prevent scientific notation, we must pass style='plain' in the ticklabel_format method.

What is exponential form in Python?

The exp() function in Python allows users to calculate the exponential value with the base set to e. Note: e is a Mathematical constant, with a value approximately equal to 2.71828. The math library must be imported for this function to be executed.

How do I customize Matplotlib?

You can customize your bar plot further by changing the outline color for each bar to be blue using the argument edgecolor and specifying a color from the matplotlib color options previously discussed. You can adjust the bar fill and edge colors of a bar plot using the arguments color and edgecolor.


2 Answers

The formatting of tick labels is controlled by a Formatter object, which assuming you haven't done anything fancy will be a ScalerFormatterby default. This formatter will use a constant shift if the fractional change of the values visible is very small. To avoid this, simply turn it off:

plt.plot(arange(0,100,10) + 1000, arange(0,100,10)) ax = plt.gca() ax.get_xaxis().get_major_formatter().set_useOffset(False) plt.draw() 

If you want to avoid scientific notation in general,

ax.get_xaxis().get_major_formatter().set_scientific(False) 

Can control this with globally via the axes.formatter.useoffset rcparam.

like image 163
tacaswell Avatar answered Oct 07 '22 20:10

tacaswell


You can use a simpler command to turn it off:

plt.ticklabel_format(useOffset=False) 
like image 26
Eki Avatar answered Oct 07 '22 21:10

Eki