0

我正在尝试删除 matplotlib 自动放在我的图表上的偏移量。例如,使用以下代码:

x=np.array([1., 2., 3.])
y=2.*x*1.e7
MyFig = plt.figure()
MyAx = MyFig.add_subplot(111)
MyAx.plot(x,y)

我得到以下结果(抱歉,我无法发布图像):y 轴的刻度为 2、2.5、3、...、6,在 y 轴的顶部有一个独特的“x10^7”。

我想从轴的顶部删除“x10^7”,并使其与每个刻度一起出现(2x10^7、2.5x10^7 等...)。如果我很好地理解了我在其他主题中看到的内容,我必须使用 use_Offset 变量。所以我尝试了以下事情:

MyFormatter = MyAx.axes.yaxis.get_major_formatter()
MyFormatter.useOffset(False)
MyAx.axes.yaxis.set_major_formatter(MyFormatter)

没有任何成功(结果不变)。难道我做错了什么?我怎样才能改变这种行为?还是我必须手动设置刻度?

提前感谢您的帮助!

4

1 回答 1

0

您可以根据需要使用FuncFormatterfromticker模块来格式化刻度标签:

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

x=np.array([1., 2., 3.])
y=2.*x*1.e7

MyFig = plt.figure()
MyAx = MyFig.add_subplot(111)

def sci_notation(x, pos):
    return "${:.1f} \\times 10^{{6}}$".format(x / 1.e7)

MyFormatter = FuncFormatter(sci_notation)

MyAx.axes.yaxis.set_major_formatter(MyFormatter)

MyAx.plot(x,y)

plt.show()

在此处输入图像描述


附带说明;出现在轴顶部的“x10^7”值不是偏移量,而是科学计数法中使用的因子。可以通过调用禁用此行为MyFormatter.use_scientific(False)。然后数字将显示为小数。

偏移量是您必须添加(或减去)到刻度值而不是乘以的值,因为后者是scale

供参考,该行

MyFormatter.useOffset(False)

应该

MyFormatter.set_useOffset(False)

因为第一个是a bool(只能有值TrueFalse),这意味着它不能作为方法调用。后者是用于启用/禁用偏移的方法。

于 2013-07-17T15:03:50.207 回答