ticklabel_format 损坏了吗? [英] Is ticklabel_format broken?

查看:21
本文介绍了ticklabel_format 损坏了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图绘制一个简单的时间序列,以年为单位在 x 轴上.不幸的是 pyplot 似乎认为这些数字应该以科学格式显示.我在 Stack Overflow 上看到了一些建议,可以通过以下方式改变这种行为:

I am trying to plot a simple time series with years as units in the x-axis. Unfortunately pyplot seems to think these numbers should be shown in scientific format. I have seen suggestions on Stack Overflow to change this behaviour with something like this:

plt.gca().ticklabel_format(style='plain', axis='x')

甚至只是

plt.ticklabel_format(style='plain', axis='x')

应该是要走的路.令我惊讶的是,我注意到这对我的系统完全没有作用.它没有效果,但也不会触发任何错误.这里有什么交易?我知道我可以设置标签字符串,但显然这不是它应该的工作方式.由于我找不到任何提及此错误的内容,因此我想检查这是否是一个常见问题.

should be the way to go. To my surprise I noticed this does absolutely nothing on my system. It has no effect, but does not trigger any errors either. What's the deal here? I know that I can set the label strings instead but clearly this is not how it's supposed to work. Since I couldn't find any mention of this bug I wanted to check if this is a common problem.

我在 Linux 上运行 python 2.7.

I'm running python 2.7 on Linux.

这基本上就是我使用的代码.除了这些值实际上是从文本文件中读出的.

This is basically the code I'm using. Except the values are actually read out of a textfile.

labels = ['1989', '1990', '1991', '1992', '1993', '1994', '1995', '1996', '1997', '1998']
years = [1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998]
values = [1.4, 2.3, 4.2, 3.2, 1.2, 3.6, 9.8, 10.2, 6.1, 3.2]

plt.plot(years, values, label='Random Data')
plt.autoscale(tight=True)
plt.title('Plot of some random data')
plt.legend(loc=0)
plt.ylabel('[some unit]')
plt.xlabel('year')

plt.ticklabel_format(style='plain', axis='x') # this should work but doesn't

plt.xticks(range(1989, 1989 + len(years)), labels) # this works but is cumbersome

plt.show()
plt.savefig('beispiel.jpg')

推荐答案

科学记数法与偏移量的解释

你得到的 (+1.989e3) 不是科学记数法,而是一个偏移量.

  • 科学记数法:在 matplotlib 中,科学记数法被视为打印在轴边缘的所有刻度值的公因子.如果您在 x 轴上绘制非常大的值,您可能会看到 1e9 或类似的打印内容.为了得到正确"您必须乘以这个系数.例如.假设您将 2.2 作为轴上的刻度值之一,1e9 打印在轴的边缘.为了得到正确"此刻度线的值,您必须将刻度值乘以公因子:2.2 * 1e9 = 2.2e9.

  • Scientific notation: In matplotlib scientific notation is seen as a common factor of all the tickvalues printed at the edge of the axis. If you plot very big values on the x-axis, you might see 1e9 or something similar printed. In order to get the "correct" value at the tickmarks you have to multiply by this factor. E.g. say you get 2.2 as one of the tickvalues on the axis, with 1e9 printed at the edge of the axis. In order to get the "correct" value of this tickmark, you must multiply the tickvalue by the common factor: 2.2 * 1e9 = 2.2e9.

偏移量:偏移量是您必须从显示的刻度值中添加/减去的值(因此 +- 旁边的数字).与科学记数法一样,这个数字也印在轴的边缘.为了得到正确"在这种情况下,刻度线处的值,您必须添加/减去此数字(如符号所示).例如.假设您将 3 作为轴上的刻度值之一,+1.989e3 打印在轴的边缘.为了得到正确"此刻度线的值,您必须添加刻度值的偏移量:3 + 1.989e3 = 1992

Offset: An offset is a value you have to add/subtract from the displayed tickvalues (hence the + or - next to the number). As with the scientific notation, this number is also printed at the edge of the axis. In order to get the "correct" value at the tickmarks in this case, you have to add/subtract this number (as indicated by the sign). E.g. say you get 3 as one of the tickvalues on the axis, with +1.989e3 printed at the edge of the axis. In order to get the "correct" value of this tickmark, you must add the offset to the tickvalue: 3 + 1.989e3 = 1992

简而言之: 科学记数法被视为您必须乘以刻度值的因素,而偏移量是您必须添加/减去刻度值的值,以便得到正确"价值.

So in short: scientific notation is seen as a factor by which you have to multiply the tickvalues, while an offset is a value you have to add/subtract to/from the tickvalues in order to get the "correct" value.

要删除打印在轴旁边的偏移量,您可以通过将 useOffset=False 传递给 ticklabel_format() 调用:

To remove the offset printed next to your axis, you can simply disable the usage of offset values by passing useOffset=False to the ticklabel_format() call:

import matplotlib.pyplot as plt

years  = [1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998]
values = [1.4, 2.3, 4.2, 3.2, 1.2, 3.6, 9.8, 10.2, 6.1, 3.2]

plt.plot(years, values, label='Random Data')
plt.autoscale(tight=True)
plt.title('Plot of some random data')
plt.legend(loc=0)
plt.ylabel('[some unit]')
plt.xlabel('year')

plt.ticklabel_format(useOffset=False)

plt.show()
plt.savefig('beispiel.jpg')

这篇关于ticklabel_format 损坏了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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