繁体   English   中英

如何使用matplotlib设置偏移量

[英]How to set offset with matplotlib

我正在尝试删除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)

没有任何成功(结果不变)。 难道我做错了什么? 我该如何改变这种行为? 还是我要手动设置刻度?

在此先感谢您的帮助!

您可以使用FuncFormatterticker模块,请你格式化ticklabels:

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)禁用此行为。 数字将显示为小数。

偏移是你必须 (或减)的tickvalues而不是繁殖 ,因为后者是一个规模的值。

供参考,行

MyFormatter.useOffset(False)

应该

MyFormatter.set_useOffset(False)

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM