简体   繁体   中英

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.stack.imgur.com/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?

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM