【问题标题】:How to prevent scientific notation in the axis offset in matplotlib?如何防止matplotlib中轴偏移的科学记数法?
【发布时间】:2022-01-22 03:13:03
【问题描述】:

我有一个 y 轴偏移量为5.223e1 的图。我宁愿只说52.23。这个可以吗?

与这个问题不同:prevent scientific notation in matplotlib.pyplot,我有兴趣从偏移本身中删除科学记数法,而不是整个轴标签。我想保留偏移量,只是不要采用科学计数法。

【问题讨论】:

  • ^ 尽管那里有很多答案显然认为问题是关于关闭偏移量,而不是保留它并只是更改格式。

标签: python matplotlib


【解决方案1】:

我认为这只能通过继承和定义你自己的formatting function来实现。
最小的例子:

import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

class my_ScalarFormatter(ScalarFormatter):
    def format_data(self, value):
        return f'{value:.2f}'

fig,(ax1,ax2) = plt.subplots(ncols=2)
ax2.yaxis.set_major_formatter(my_ScalarFormatter())
ax1.set_ylim(52.23, 52.231)
ax2.set_ylim(52.23, 52.231)
ax1.set_title('Default Formatter')
ax2.set_title('Custom Formatter')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-10
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多