这是pandas 0.17中`Series.plot`的错误吗?

时间:2015-11-06 04:55:23

标签: python pandas plot

我总是使用ser.plot(xticks=range(0, len(ser), step), ..)来绘制带有指定xticks的数字。但是当带有标签的Series时,它在pandas 0.17中不再起作用了。以下是代码:

In [1]: from numpy import random as rnd 
In [2]: import pandas as pd
In [3]: pd.__version__
Out[3]: '0.17.0'
In [4]: %matplotlib inline
In [5]: rnd.seed(123)
In [6]: ser = pd.Series(rnd.randn(73).cumsum(), index=['P%02d' % i for i in range(73)])
In [7]: ser.plot(figsize=(9, 6), rot=60, title='Figure without xticks')
Out[7]: <matplotlib.axes._subplots.AxesSubplot at 0x8370198

enter image description here

In [8]ser.plot(figsize=(9, 6), xticks=list(range(0, 73, 6)), rot=60, title='Figure with xticks')
Out[8]: <matplotlib.axes._subplots.AxesSubplot at 0x83e9940>

enter image description here

pandas 0.16中的相同代码:

In [1]: from numpy import random as rnd
In [2]: import pandas as pd
In [3]: pd.__version__
Out[3]: '0.16.0'
In [4]: %matplotlib inline
In [5]: rnd.seed(123)
In [6]: ser = pd.Series(rnd.randn(73).cumsum(), index=['P%02d' % i for i in range(73)])
In [8]: ser.plot(figsize=(9, 6), xticks=list(range(0, 73, 6)), rot=60, title='Figure with xticks')
Out[8]: <matplotlib.axes._subplots.AxesSubplot at 0xbbf19b0>

enter image description here

1 个答案:

答案 0 :(得分:3)

这确实是0.17.0的回归,并将在0.17.1中修正(见https://github.com/pydata/pandas/pull/11531)。

如果您现在需要解决方法(直到0.17.1发布或者为了防止您需要降级),您可以:

  • 使用以下方法创建绘图后手动调整刻度线标题:

    ax.set_xticklabels(['P%02d' % i for i in ax.get_xticks()])
    
  • 或在pandas代码中应用补丁(请参阅上面的链接,它只有一行更改)