禁用 pandas .p​​lot() 函数中的科学记数法和偏移量

Veg*_*ega 3 python plot scientific-notation pandas

我有一个数据框 df ,其中有 2 列,我想将其绘制在一起,并以天数作为索引:

           | col1  | col2   | col3 | ...
2020-01-01 | 1     | 300000 | ...
2020-01-02 | 1000  | 600000 | ...
2020-01-03 | 3000  | 50000  | ...
Run Code Online (Sandbox Code Playgroud)

通过绘制 col1 + col2

df[["col1", "col2"]].plot()
Run Code Online (Sandbox Code Playgroud)

显示从 0 到 1.0 的值,并在顶部“1e6”,如下例所示: https: //i.stack.imgur.com/tJjgX.png

我想要 y 轴上的完整值范围,而不是科学记数法。我如何通过 pandas .p​​lot() 或 matplotlib 来做到这一点?

Geo*_*eom 6

您有多种选择:

选项一:使用 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
Run Code Online (Sandbox Code Playgroud)

选项二:更改 Pandas 设置

pd.set_option('display.float_format', lambda x: '%.3f' % x)
Run Code Online (Sandbox Code Playgroud)

或者

pd.options.display.float_format = '{:.2f}'.format
Run Code Online (Sandbox Code Playgroud)

我相信选项一更好,因为您只需要它来绘制图表,而不必修改数据框列。

干杯!