在 pandas .plot() 函数中禁用科学记数法和偏移量

问题描述 投票:0回答:4

我有一个数据框 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.sstatic.net/tJjgX.png

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

python pandas plot scientific-notation
4个回答
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

选项二:更改 Pandas 设置

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

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

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

干杯!


4
投票

您可以尝试更改

axes.formatter.limits
rcParams
属性:

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

如果轴范围的 log10 小于第一个或大于第二个,Matplotlib 使用科学计数法。

您可以自定义所需输出的下限和上限。


0
投票

非常简单。只需使用“ylim”并给出原始数据的范围。 这为我解决了这个问题!

dataframe.plot.scatter(x='NoOfStaff', y='费用', c='CovidYesNo', cmap='rainbow', ylim=(0,100000))


-3
投票

使用

.set_ylim([min,max])
设置绘图的 y 轴限制

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.