[英]How to set offset with matplotlib
I'm trying to remove the offset that matplotlib automatically put on my graphs. 我正在尝试删除matplotlib自动放置在图形上的偏移量。 For example, with the following code:
例如,使用以下代码:
x=np.array([1., 2., 3.])
y=2.*x*1.e7
MyFig = plt.figure()
MyAx = MyFig.add_subplot(111)
MyAx.plot(x,y)
I obtain the following result (sorry, I cannot post image): the y-axis have the ticks 2, 2.5, 3, ..., 6, with a unique "x10^7" at the top of the y axis. 我得到以下结果(很抱歉,我无法发布图像):y轴的刻度为2、2.5、3,...,6,在y轴的顶部带有唯一的“ x10 ^ 7”。
I would like to remove the "x10^7" from the top of the axis, and making it appearing with each tick (2x10^7, 2.5x10^7, etc...). 我想从轴的顶部删除“ x10 ^ 7”,并使其与每个刻度一起出现(2x10 ^ 7、2.5x10 ^ 7等)。 If I understood well what I saw in other topics, I have to play with the use_Offset variable.
如果我很好地理解了其他主题中的内容,则必须使用use_Offset变量。 So I tried the following thing:
因此,我尝试了以下操作:
MyFormatter = MyAx.axes.yaxis.get_major_formatter()
MyFormatter.useOffset(False)
MyAx.axes.yaxis.set_major_formatter(MyFormatter)
without any success (result unchanged). 没有任何成功(结果不变)。 Am I doing something wrong?
难道我做错了什么? How can I change this behaviour?
我该如何改变这种行为? Or have I to manually set the ticks ?
还是我要手动设置刻度?
Thanks by advance for any help ! 在此先感谢您的帮助!
You can use the FuncFormatter
from the ticker
module to format the ticklabels as you please: 您可以使用
FuncFormatter
从ticker
模块,请你格式化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()
On a side note; 附带说明; the "x10^7" value that appears at the top of your axis is not an offset, but a factor used in scientific notation.
出现在轴顶部的“ x10 ^ 7”值不是偏移量,而是科学计数法中使用的一个因素。 This behavior can be disabled by calling
MyFormatter.use_scientific(False)
. 可以通过调用
MyFormatter.use_scientific(False)
禁用此行为。 Numbers will then be displayed as decimals. 数字将显示为小数。
An offset is a value you have to add (or subtract) to the tickvalues rather than multiply with, as the latter is a scale . 偏移是你必须加 (或减)的tickvalues而不是繁殖 ,因为后者是一个规模的值。
For reference, the line 供参考,行
MyFormatter.useOffset(False)
should be 应该
MyFormatter.set_useOffset(False)
as the first one is a bool
(can only have the values True
or False
), which means it can not be called as a method. 因为第一个是
bool
(只能具有值True
或False
),这意味着它不能作为方法调用。 The latter is the method used to enable/disable the offset. 后者是用于启用/禁用偏移的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.