当我执行以下操作时,我遇到了自动x轴重新缩放的问题:
第二个图为我保留了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*')
答案 0 :(得分: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)