在 pandas .plot() 函数中禁用科学记数法和偏移量
Disable scientific notation and offset in pandas .plot() function
我有一个数据框 df,其中有 2 列我想一起绘制,天数作为索引:
| col1 | col2 | col3 | ...
2020-01-01 | 1 | 300000 | ...
2020-01-02 | 1000 | 600000 | ...
2020-01-03 | 3000 | 50000 | ...
通过
绘制col1 + col2
df[["col1", "col2"]].plot()
显示从 0 到 1.0 的值以及顶部的“1e6”,如本例所示:https://i.stack.imgur.com/tJjgX.png
我想要 y 轴上的完整值范围并且没有科学记数法。我如何通过 pandas .plot() 或 matplotlib 执行此操作?
使用.set_ylim([min,max])
设置绘图的y轴限制
您有多种选择:
选项一:使用 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
选项二:更改Pandas设置
pd.set_option('display.float_format', lambda x: '%.3f' % x)
或
pd.options.display.float_format = '{:.2f}'.format
我认为选项一更好,因为您只需要它用于图形,不一定要修改数据框列。
干杯!
您可以尝试更改 axes.formatter.limits
属性 of rcParams
:
plt.rcParams["axes.formatter.limits"] = (-5, 12)
如果坐标轴范围的 log10 小于第一个或大于第二个,Matplotlib 将使用科学记数法。
您可以自定义所需输出的下限和上限。
我有一个数据框 df,其中有 2 列我想一起绘制,天数作为索引:
| col1 | col2 | col3 | ...
2020-01-01 | 1 | 300000 | ...
2020-01-02 | 1000 | 600000 | ...
2020-01-03 | 3000 | 50000 | ...
通过
绘制col1 + col2df[["col1", "col2"]].plot()
显示从 0 到 1.0 的值以及顶部的“1e6”,如本例所示:https://i.stack.imgur.com/tJjgX.png
我想要 y 轴上的完整值范围并且没有科学记数法。我如何通过 pandas .plot() 或 matplotlib 执行此操作?
使用.set_ylim([min,max])
设置绘图的y轴限制
您有多种选择:
选项一:使用 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
选项二:更改Pandas设置
pd.set_option('display.float_format', lambda x: '%.3f' % x)
或
pd.options.display.float_format = '{:.2f}'.format
我认为选项一更好,因为您只需要它用于图形,不一定要修改数据框列。
干杯!
您可以尝试更改 axes.formatter.limits
属性 of rcParams
:
plt.rcParams["axes.formatter.limits"] = (-5, 12)
如果坐标轴范围的 log10 小于第一个或大于第二个,Matplotlib 将使用科学记数法。
您可以自定义所需输出的下限和上限。