如何防止在 Python matplotlib 图中将数字更改为指数形式 [英] How to prevent numbers being changed to exponential form in Python matplotlib figure

查看:45
本文介绍了如何防止在 Python matplotlib 图中将数字更改为指数形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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).

我更喜欢我的图形保留轴的简单编号,而不是使用指数形式.有没有办法强制 Matplotlib 这样做?

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?

推荐答案

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

The formatting of tick labels is controlled by a Formatter object, 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:

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)

可以通过 axes.formatter.useoffset rcparam 全局控制它.

Can control this with globally via the axes.formatter.useoffset rcparam.

这篇关于如何防止在 Python matplotlib 图中将数字更改为指数形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆