Python matplotlib轴标签的偏移因子和偏移

Python matplotlib轴标签的偏移因子和偏移,python,matplotlib,Python,Matplotlib,在matplotlib中的轴刻度标签上,有两种可能的偏移:因子和偏移: 在右下角,1e-8为“因子”,1.441249698e1为“移位” 这里有很多答案,说明了如何处理这两个问题: 我只想取消轮班,但似乎不知道怎么做。因此matplotlib应该只允许缩放我的轴,而不允许移动零点是否有一种简单的方法来实现此行为?您可以固定显示在轴上的数量级,如中所示。其思想是将普通的ScalarFormatter子类化,并固定要显示的数量级。然后将usecoffset设置为False将防止显示某些

在matplotlib中的轴刻度标签上,有两种可能的偏移:因子偏移

在右下角,1e-8为“因子”,1.441249698e1为“移位”

这里有很多答案,说明了如何处理这两个问题:


我只想取消轮班,但似乎不知道怎么做。因此matplotlib应该只允许缩放我的轴,而不允许移动零点是否有一种简单的方法来实现此行为?

您可以固定显示在轴上的数量级,如中所示。其思想是将普通的
ScalarFormatter
子类化,并固定要显示的数量级。然后将
usecoffset
设置为False将防止显示某些偏移,但仍会显示系数

格式
“%1.1f”
将只显示一位小数。最后,使用
MaxNLocator
可以设置轴上的最大刻度数

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker

class OOMFormatter(matplotlib.ticker.ScalarFormatter):
    def __init__(self, order=0, fformat="%1.1f", offset=False, mathText=True):
        self.oom = order
        self.fformat = fformat
        matplotlib.ticker.ScalarFormatter.__init__(self,useOffset=offset,useMathText=mathText)
    def _set_orderOfMagnitude(self, nothing):
        self.orderOfMagnitude = self.oom
    def _set_format(self, vmin, vmax):
        self.format = self.fformat
        if self._useMathText:
            self.format = '$%s$' % self.format

x = [0.6e-8+14.41249698, 3.4e-8+14.41249698]
y = [-7.7e-11-1.110934954e-2, -0.8e-11-1.110934954e-2]

fig, ax = plt.subplots()
ax.plot(x,y)

y_formatter = OOMFormatter(-2, "%1.1f")
ax.yaxis.set_major_formatter(y_formatter)
x_formatter = OOMFormatter(1, "%1.1f")
ax.xaxis.set_major_formatter(x_formatter)

ax.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator(2))
ax.yaxis.set_major_locator(matplotlib.ticker.MaxNLocator(2))

plt.show()


请记住,此图是不准确的,您不应该在您交给其他人的任何类型的报告中使用此图,因为他们不知道如何解释此图。

当问题无法转移时,您实际上不清楚如何标记刻度。请明确说明您希望在轴上显示哪些数字,以及下面显示哪些数字(作为系数)?@ImportanceOfBeingErnest确实,对此表示抱歉。在这个y轴示例中,我希望它在所有轴标记上显示1.1,并将1e-2作为因子。或者,仅用1.1和1e-2作为因子的一个单轴勾号(即,在这两种情况下,表明小差异的分辨率无关)。