ticklabel_format是否损坏? [英] Is ticklabel_format broken?

查看:227
本文介绍了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传递给

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天全站免登陆