【问题标题】:How to multiply axis tick values by factors of 10 in Matplotlib如何在 Matplotlib 中将轴刻度值乘以 10 的因数
【发布时间】:2021-06-12 06:04:47
【问题描述】:

我想将我的 y 轴刻度相乘(如下所示),以便它们显示从 2500 到 20000 的值范围,而不是 0.25 到 2。

我该怎么做?

【问题讨论】:

标签: python pandas numpy matplotlib jupyter-notebook


【解决方案1】:

你可以使用这个order of magnitude formatter:

# https://stackoverflow.com/a/42658124/13138364
import matplotlib.ticker

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat='%1.1f', offset=True, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_order_of_magnitude(self):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin=None, vmax=None):
        self.format = self.fformat
        if self._useMathText:
            self.format = r'$\mathdefault{%s}$' % self.format

1e7 更改为1e3 的玩具示例:

x = np.arange(100)
y = np.random.rand(100) * 2e7

# plot with `ax` handle
fig, ax = plt.subplots()
ax.plot(x, y)

# format order of magnitude of `ax.yaxis`
order = 3
ax.yaxis.set_major_formatter(OOMFormatter(order, '%1.1f'))

之前/之后:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 1970-01-01
    • 1970-01-01
    • 2013-02-21
    • 2017-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多