gpt4 book ai didi

python - 阻止 pandas plot 进行新的 x 轴布局

转载 作者:行者123 更新时间:2023-12-05 08:00:55 25 4
gpt4 key购买 nike

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

最佳答案

显式创建无花果和坐标区对象,以便更好地控制它们。在这种情况下,使用轴对象的 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)

关于python - 阻止 pandas plot 进行新的 x 轴布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16599797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com