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

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

问题描述

我在Python中使用Matplotlib来绘制简单的x-y数据集。虽然当我使用图视图(在执行 plt.show()),x轴值从标准数字形式(1050,1060,1070等)变为具有指数符号的科学形式(例如1,1.5,2.0,其中x轴标签给出为 + 1.057e3 )。



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

解决方案

滴答标签的格式由 Formatter object,假设你没有做任何事情,它会是 ScalerFormatter 默认。如果可见值的分数变化非常小,则此格式化程序将使用常量移位。为避免这种情况,只需将其关闭:

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

c $ c $> ax.get_xaxis()。get_major_formatter()。set_scientific(False)

可以控制这个全球通过 axes.formatter.useoffset rcparam。


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

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?

解决方案

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)

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

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

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