停止熊猫绘图做新的x轴布局



当我执行以下操作时,我有一个自动x轴重新缩放的问题:

  1. 图列1
  2. 绘制第1列,其中第2列不是全列,但样式不同。

第二个图一直在为我重新缩放x轴,失去了完整的第1列图的概述。我在这个笔记本上详细记录了发生的事情:http://nbviewer.ipython.org/5596249

是否有一种方法可以防止x轴在第二次绘图时重新缩放?我试过x_compat=True,但这似乎对我没有任何帮助。

版本号:pd: 0.11.0, MPL: 1.2.0

[编辑]下面是python的输出:

In [1]: import pandas as pd
In [2]: pd.__version__
Out[2]: '0.12.0.dev-f354548'
In [3]: dr = pd.date_range('now', periods=10)
In [4]: df = pd.DataFrame(randn(10,2), index=dr)
In [5]: df
Out[5]:
                            0         1
2013-05-17 17:55:43 -0.440814  0.246620
2013-05-18 17:55:43 -0.732045 -0.896267
2013-05-19 17:55:43  1.131248  1.213163
2013-05-20 17:55:43  0.478372  0.624647
2013-05-21 17:55:43  1.425489  0.396689
2013-05-22 17:55:43 -0.881991  0.322917
2013-05-23 17:55:43 -1.047584  0.154040
2013-05-24 17:55:43 -0.327258  0.944843
2013-05-25 17:55:43 -0.013396  1.045499
2013-05-26 17:55:43 -0.035380 -0.611224
In [6]: df[1][:5]=nan
In [7]: df
Out[7]:
                            0         1
2013-05-17 17:55:43 -0.440814       NaN
2013-05-18 17:55:43 -0.732045       NaN
2013-05-19 17:55:43  1.131248       NaN
2013-05-20 17:55:43  0.478372       NaN
2013-05-21 17:55:43  1.425489       NaN
2013-05-22 17:55:43 -0.881991  0.322917
2013-05-23 17:55:43 -1.047584  0.154040
2013-05-24 17:55:43 -0.327258  0.944843
2013-05-25 17:55:43 -0.013396  1.045499
2013-05-26 17:55:43 -0.035380 -0.611224
In [8]: df[0].plot()
Out[8]: <matplotlib.axes.AxesSubplot at 0x116dbff50>
In [9]: df[1].plot()
Out[9]: <matplotlib.axes.AxesSubplot at 0x116dbff50>
In [10]: # the above did not rescale the x-axis, even so the plottable range of [1] is smaller
In [11]: clf()
In [12]: df['selector'] = df[1].notnull()
In [13]: df
Out[13]:
                            0         1 selector
2013-05-17 17:55:43 -0.440814       NaN    False
2013-05-18 17:55:43 -0.732045       NaN    False
2013-05-19 17:55:43  1.131248       NaN    False
2013-05-20 17:55:43  0.478372       NaN    False
2013-05-21 17:55:43  1.425489       NaN    False
2013-05-22 17:55:43 -0.881991  0.322917     True
2013-05-23 17:55:43 -1.047584  0.154040     True
2013-05-24 17:55:43 -0.327258  0.944843     True
2013-05-25 17:55:43 -0.013396  1.045499     True
2013-05-26 17:55:43 -0.035380 -0.611224     True
In [14]: df[0].plot()
Out[14]: <matplotlib.axes.AxesSubplot at 0x111288250>
In [15]: df[df.selector][0].plot(style='r*', markersize=5)
Out[15]: <matplotlib.axes.AxesSubplot at 0x111288250>
In [16]: # the above rescaled the x-axis

下面是代码:

import pandas as pd
pd.__version__
dr = pd.date_range('now',periods=10)
df = pd.DataFrame(randn(10,2),index=dr)
df[1][:5]=nan
# This way it works:
df[0].plot()
df[1].plot()

df['selector'] = df[1].notnull()
df[0].plot()
# This way the x-axis is being rescaled. How can I fix it at the previous setting?
df[df.selector][0].plot(style='r*')

显式创建您的图形和轴对象,以便您可以更好地控制它们。在本例中,使用axes对象

autoscale方法
import matplotlib.pyplot as plt
import pandas
# [make a dataframe called df]
fig, ax = plt.subplots()
df['A'].plot(ax=ax)
ax.autoscale(enable=False)
df['B'].plot(ax=ax)

最新更新