pandas 熊猫在一个尺度上绘制两个图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17066313/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas plotting two graphs on one scale
提问by Joop
I have a line graph that I am highligting at end with a marker(shown as large red diamond here).
我有一个折线图,最后用标记高亮显示(此处显示为大红色菱形)。
I am using two two pandas plot commands to create it. Problem is that I get unexpected results. depending on the length of the data and whether I put the plot for the red diamond first or second I get different results. There does not seem to be a pattern that I can discern. The Correct / Expected result shown below
我正在使用两个两个 Pandas plot 命令来创建它。问题是我得到了意想不到的结果。根据数据的长度以及我是将红色菱形图放在第一位还是第二位,我会得到不同的结果。似乎没有我可以辨别的模式。正确/预期结果如下所示


sometimes i get:
有时我得到:


and most of the time with big data sets I get the following warning:
大多数情况下,使用大数据集时,我会收到以下警告:
/Users/xxxxx/.virtualenvs/test2/lib/python2.7/site-packages/matplotlib/axes.py:2542: UserWarning: Attempting to set identical left==right results in singular transformations; automatically expanding. left=15727, right=15727 + 'left=%s, right=%s') % (left, right))
/Users/xxxxx/.virtualenvs/test2/lib/python2.7/site-packages/matplotlib/axes.py:2542:UserWarning:尝试设置相同的 left==right 导致奇异变换;自动扩展。left=15727, right=15727 + 'left=%s, right=%s') % (left, right))
The Warning only shows 1st time it happens. Obviously pandas does not like support the plotting of 2 different series with different x scales on the same axis?
警告仅显示第一次发生。显然大Pandas不喜欢支持在同一轴上绘制具有不同 x 比例的 2 个不同系列?
Can try code below to generate the graphs, can play around by passing, Series or Dataframes for plot can also reverse the order of the plotting of the red diamond. Can also change the number of data points. One error that I could not recreate here is with the red diamond in middle and blue line only going of to left.
可以尝试下面的代码来生成图形,可以通过传递,Series 或 Dataframes for plot 也可以颠倒红色菱形的绘图顺序。也可以改变数据点的数量。我无法在此处重新创建的一个错误是中间的红色菱形和蓝色线仅向左移动。
Code:
代码:
plot_with_series = False
reverse_order = False
import pandas as pd
dates = pd.date_range('20101115', periods=800)
df = pd.DataFrame(randn(len(dates)), index = dates, columns = ['A'])
ds = pd.Series(randn(len(dates)), index = dates)
clf()
if plot_with_series:
if reverse_order: ds.plot()
ds.tail(1).plot(style='rD', markersize=20)
if not reverse_order: ds.plot()
else:
if reverse_order: df.plot(legend=False)
df.A.tail(1).plot(style='rD', markersize=20,legend=False)
if not reverse_order: df.plot(legend=False)
The errors/warnings happen from both within IPython or from running the as script from command line. Also constant across 2 latest versions of pandas. Any ideas or obvious problems?
错误/警告发生在 IPython 内或从命令行运行 as 脚本。在 2 个最新版本的Pandas中也保持不变。任何想法或明显的问题?
回答by Rutger Kassies
I think pandas by default creates a new plot instead of using the 'active' plot. Capturing the axes and passing it to the next plotting command works fine for me, and is the way to go if you want to reuse your axes.
我认为Pandas默认创建一个新图而不是使用“活动”图。捕获轴并将其传递给下一个绘图命令对我来说很好用,如果你想重用你的轴,这是要走的路。
Change the last two lines in your example to:
将示例中的最后两行更改为:
ax = df.A.tail(1).plot(style='rD', markersize=20,legend=False)
if not reverse_order: df.plot(legend=False, ax=ax)
Difference is that the axes returned by matplotlib (via pandas) is captured, and passed again with ax=ax. Its also more conform the preferred OO-style for using matplotlib.
不同之处在于 matplotlib(通过 pandas)返回的轴被捕获,并再次通过ax=ax. 它也更符合使用 matplotlib 的首选 OO 风格。
回答by Joop
Agree with previous answer, but also adding another way. Adapted from the official pandas documentation on plotting http://pandas.pydata.org/pandas-docs/stable/visualization.html
I just adjusted the second column of the DataFrame to be only the last point on a padded nan column.
同意之前的答案,但也增加了另一种方式。改编自
Pandas官方文档关于绘图http://pandas.pydata.org/pandas-docs/stable/visualization.html
我只是将 DataFrame 的第二列调整为仅填充 nan 列的最后一点。
df['B'] = np.nan
df['B'][-1] = df.A[-1] # Just 1 datapoint
plt.figure()
with pd.plot_params.use('x_compat', True):
df.A.plot(color='b')
df.B.plot(style='rD', markersize=12)

