Marking Event Points on a Pandas plot

跟風遠走 提交于 2019-12-11 03:06:45

问题


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.

Edit: 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")

回答1:


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)

Giving

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.



来源:https://stackoverflow.com/questions/21638727/marking-event-points-on-a-pandas-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!