在 pandas 图上标记事件点 [英] Marking Event Points on a Pandas plot

查看:99
本文介绍了在 pandas 图上标记事件点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在绘制一些二维时间序列数据,并且我想用X或其他标记在图中标出特定的日期.例如,如果我拥有过去6个月中每个日期的所有数据,并且我想指出圣诞节在该图上的位置.什么是最简单的方法来做到这一点?我可以手动完成此操作,但希望在代码中添加一些可以自动执行的操作.

I'm plotting some 2-D time series data, and I want to mark specific dates on the plot with an X or some other marker. For example, if I have all the data for each date running for the last 6 months, and I want to point out where Christmas was on this plot. What would be the easiest way to accomplish this? I can do it manually, but would prefer to add something that automates it in my code.

到目前为止,我仍在尝试.不知道是否有什么会更好.比较日期对象时遇到了一些麻烦,这就是为什么将比较字符串按原样拆分的原因.

My attempt at this so far. Not sure if there is anything the would work better. I had some trouble comparing the date objects, which is why the comparison string is split up as it is.

filt1.plot(figsize=(16,16))
filt3=filtered[(filtered.index.year==2012)&(filtered.index.month==12)&(filtered.index.day==25)]["Income_MA"]
filt3.plot(style="ro")

推荐答案

跟随tcaswell的评论,类似的内容可能对您有用?

Following up on tcaswell's comment, something like this might work for you?

df = pd.Series(np.random.rand(180), index=pd.date_range(start=pd.datetime(2000, 9, 1), periods=180, freq='D'))
xmas_date = pd.datetime(2000, 12, 25)

myax = df.cumsum().plot()
ylims = myax.get_ylim()
myax.vlines(xmas_date, ylims[0], ylims[1])
myax.annotate("Xmas", xy=(xmas_date, ylims[1]), 
              xytext=(-12, 5),
              textcoords='offset points')
myax.set_ylim(ylims)

给予

这是一个脆弱的解决方案,因为我已经手动设置了一个用于注释的日期,但是将代码包装到函数中并在您感兴趣的每个日期都调用它应该非常容易.

This is a fragile solution because I've manually set a single date to annotate but it should be pretty easy to wrap that code in a function and call it for every date you're interested in.

这篇关于在 pandas 图上标记事件点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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