pandas 在一个比例尺上绘制两个图形 [英] Pandas plotting two graphs on one scale

查看:167
本文介绍了 pandas 在一个比例尺上绘制两个图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个折线图,最后用一个标记高亮(这里显示为大红色菱形).

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

有时候我会得到:

在大多数情况下,使用大数据集时,我会收到以下警告:

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:尝试设置相同的left == right结果 单数转换;自动扩展. 左= 15727,右= 15727 +'left =%s,right =%s')%(left,right))

/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))

警告仅在第一次发生时显示.显然,熊猫不喜欢支持在同一轴上绘制具有不同x比例的2个不同系列的图吗?

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?

可以尝试下面的代码来生成图形,可以通过传递,序列或数据框进行绘制,也可以颠倒红色菱形的绘制顺序.也可以更改数据点的数量.我在这里无法重现的一个错误是中间的红色菱形和蓝色的线只向左移动.

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.

代码:

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)

错误/警告发生在IPython内部或从命令行运行as脚本.在2个最新版本的熊猫中也保持不变.有什么想法或明显的问题吗?

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?

推荐答案

我认为熊猫默认情况下会创建一个新情节,而不是使用活动"情节.捕获轴并将其传递给下一个绘图命令对我来说很好用,这是您要重复使用轴的一种方式.

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.

将示例中的最后两行更改为:

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)

区别在于,捕获了matplotlib返回的轴(通过熊猫),并再次通过ax=ax传递.它也更符合使用matplotlib的首选OO风格.

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.

这篇关于 pandas 在一个比例尺上绘制两个图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆