Vega
Vega

Reputation: 2929

Disable scientific notation and offset in pandas .plot() function

I have a dataframe df with 2 columns I want to plot together and days as index:

           | col1  | col2   | col3 | ...
2020-01-01 | 1     | 300000 | ...
2020-01-02 | 1000  | 600000 | ...
2020-01-03 | 3000  | 50000  | ...

Plotting col1 + col2 via

df[["col1", "col2"]].plot()

shows values from 0 till 1.0 and at the top "1e6" like in this example: https://i.sstatic.net/tJjgX.png

I want the full value range on the y-axis and no scientific notation. How can I do this via pandas .plot() or matplotlib?

Upvotes: 3

Views: 8459

Answers (3)

AYUSH PANDEY
AYUSH PANDEY

Reputation: 41

You can try changing the axes.formatter.limits property of rcParams:

plt.rcParams["axes.formatter.limits"] = (-5, 12)

Matplotlib uses scientific notation if log10 of the axis range is smaller than the first or larger than the second.

You can customize the lower and upper limits for desired output.

Upvotes: 4

Geom
Geom

Reputation: 1546

You have several options:

Option One: With Matplotlib

axes=fig.add_axes([0,0,1,1])
axes.set_xticks() # with list or range() inside
axes.set_yticks() # with list or range() inside

#You can also label the ticks with your desired values
axes.set_xticklabels() # with list or range() inside
axes.set_yticklabels() # with list or range() inside

Option Two: Change Pandas settings

pd.set_option('display.float_format', lambda x: '%.3f' % x)

or

pd.options.display.float_format = '{:.2f}'.format

I believe option one is better since you need it only for the graph and don't necessarily want to modify your dataframe columns.

Cheers!

Upvotes: 6

Arpit
Arpit

Reputation: 394

Use .set_ylim([min,max]) for setting the y axis limit of the plot

Upvotes: -3

Related Questions