在可视化时间序列时标记特定日期 [英] Marking specific dates when visualizing a time series

查看:42
本文介绍了在可视化时间序列时标记特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含几年数据的时间序列,例如:

  ts = pd.Series(np.random.randn(1000),index = pd.date_range('1/1/2000',period = 1000))ts = ts.cumsum()ts.plot()

我还有两个额外的数组:让我们调用第一个

dates = [pd.datetime("2000-12-01"), pd.datetime("2001-01-03")]

第二个

labels = ["我的生日", "我爸爸的生日"]

labels[i] 包含日期 [i] 的标签.我想做的是将它们显示在时间序列图中,以便识别它们.一种可能的可视化方法是在x轴上显示日期,从此处开始绘制一条垂直线,并在图例中(带有颜色编码)或该行旁边的某个地方放置标签.

最终结果应该与此没什么不同:

解决方案

在 pandas 和 matplotlib API 之间切换一开始可能会令人困惑.

解决方案:获取当前轴,然后使用标准的matplotlib API进行注释.这使您开始:

将pandas导入为pd将numpy导入为np导入matplotlib.pyplot作为pltts = pd.Series(np.random.randn(1000),index=pd.date_range('1/1/2000',周期=1000))ts = ts.cumsum()ts.plot()label_list = [(pd.to_datetime("2001-05-01"),'我\ nbirthday','r'),(pd.to_datetime("2001-10-16"), "爸爸的\n生日", 'b')]ax = plt.gca()对于日期点、标签、标签列表中的 clr:plt.axvline(x=date_point,颜色=clr)plt.text(date_point, ax.get_ylim()[1]-4, 标签,horizo​​ntalalignment ='center',垂直对齐='中心',颜色=clr,bbox = dict(facecolor ='white',alpha = 0.9))plt.show()

这将产生下面的图像,您需要研究修改

I have a time series that has a few years' worth of data, for example this:

ts = pd.Series(np.random.randn(1000), index=pd.date_range('1/1/2000', periods=1000))

ts = ts.cumsum()

ts.plot()

I also have two extra arrays: let's call the first

dates = [pd.datetime("2000-12-01"), pd.datetime("2001-01-03")]

And the second

labels = ["My birthday", "My dad's birthday"]

labels[i] contains the label for dates[i]. What I'd like to do is to display them in the time series graph so that they can be recognized. One possible visualization could be to display the date on the x axis, draw a vertical line starting from there and have the label either in a legend (with color coding) or somewhere next to the line.

The end result shouldn't be too different from this:

解决方案

Switching between pandas and matplotlib APIs can be confusing at first.

The solution: get the current axis and then use standard matplotlib API to annotate. This starts you off:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

ts = pd.Series(np.random.randn(1000),
               index=pd.date_range('1/1/2000',
               periods=1000))

ts = ts.cumsum()
ts.plot()

label_list = [
    (pd.to_datetime("2001-05-01"), 'My\nbirthday', 'r'),
    (pd.to_datetime("2001-10-16"), "Dad's\nbirthday", 'b')
]

ax = plt.gca()

for date_point, label, clr in label_list:
    plt.axvline(x=date_point, color=clr)
    plt.text(date_point, ax.get_ylim()[1]-4, label,
             horizontalalignment='center',
             verticalalignment='center',
             color=clr,
             bbox=dict(facecolor='white', alpha=0.9))

plt.show()

This produces the image below, and you need to look into modifying titles, and text labels and their bounding boxes to the axis object:

这篇关于在可视化时间序列时标记特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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