如何防止在 Python matplotlib 图中将数字更改为指数形式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14711655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:13:30  来源:igfitidea点击:

How to prevent numbers being changed to exponential form in Python matplotlib figure

pythongraphmatplotlibfigure

提问by IanRoberts

I'm using Matplotlib in Python to plot simple x-y datasets. This produces nice-looking graphs, although when I "zoom in" too close on various sections of the plotted graph using the Figure View (which appears when you execute plt.show()), the x-axis values change from standard number form (1050, 1060, 1070 etc.) to scientific form with exponential notation (e.g. 1, 1.5, 2.0 with the x-axis label given as +1.057e3).

我在 Python 中使用 Matplotlib 来绘制简单的 xy 数据集。这会生成漂亮的图形,尽管当我使用图形视图(在您执行 时出现plt.show())在绘制图形的各个部分“放大”太近时,x 轴值会从标准数字形式(1050、1060、 1070 等)转换为具有指数符号的科学形式(例如 1、1.5、2.0,x 轴标签为+1.057e3)。

I'd prefer my figures to retain the simple numbering of the axis, rather than using exponential form. Is there a way I can force Matplotlib to do this?

我更喜欢我的数字保留轴的简单编号,而不是使用指数形式。有没有办法可以强制 Matplotlib 执行此操作?

采纳答案by tacaswell

The formatting of tick labels is controlled by a Formatterobject, which assuming you haven't done anything fancy will be a ScalerFormatterby default. This formatter will use a constant shift if the fractional change of the values visible is very small. To avoid this, simply turn it off:

刻度标签的格式由Formatter对象控制,假设您没有做任何花哨的事情ScalerFormatter,默认情况下将是. 如果可见值的小数变化非常小,则此格式化程序将使用常量移位。为避免这种情况,只需将其关闭:

plt.plot(arange(0,100,10) + 1000, arange(0,100,10))
ax = plt.gca()
ax.get_xaxis().get_major_formatter().set_useOffset(False)
plt.draw()

If you want to avoid scientific notation in general,

如果你想避免一般的科学记数法,

ax.get_xaxis().get_major_formatter().set_scientific(False)

Can control this with globally via the axes.formatter.useoffsetrcparam.

可以通过axes.formatter.useoffsetrcparam全局控制它。

回答by Animesh Saxena

You can use something like:

您可以使用以下内容:

from matplotlib.ticker import ScalarFormatter, FormatStrFormatter

ax.xaxis.set_major_formatter(FormatStrFormatter('%.0f'))

回答by Eki

You can use a simpler command to turn it off:

您可以使用更简单的命令将其关闭:

plt.ticklabel_format(useOffset=False)