3

当我执行以下操作时,我遇到了自动 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

[编辑] 这是 ipython 输出:

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*')
4

1 回答 1

0

显式创建 fig 和 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)
于 2013-05-17T16:54:35.027 回答