【问题标题】:Disable scientific notation and offset in pandas .plot() function在 pandas .p​​lot() 函数中禁用科学记数法和偏移
【发布时间】:2020-08-12 08:23:30
【问题描述】:

我有一个数据框 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 .p​​lot() 或 matplotlib 做到这一点?

【问题讨论】:

    标签: python pandas plot scientific-notation


    【解决方案1】:

    您有多种选择:

    选项一:使用 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
    

    我相信选项一会更好,因为您只需要它用于图表并且不一定要修改您的数据框列。

    干杯!

    【讨论】:

      【解决方案2】:

      您可以尝试更改rcParamsaxes.formatter.limits 属性:

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

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

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

      【讨论】:

        【解决方案3】:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-08-29
          • 1970-01-01
          • 2021-05-01
          • 1970-01-01
          • 1970-01-01
          • 2020-01-23
          • 1970-01-01
          • 2022-01-22
          相关资源
          最近更新 更多