【发布时间】:2016-01-12 11:41:55
【问题描述】:
我的数据存储为 pandas 数据框。我使用 matplotlib 中的 plot_date() 函数创建了日期(对象格式)与百分位数(int64 格式)的图表,并希望在预先指定的日期添加一些垂直线。
我已经设法在预先指定的日期添加点标记,但似乎找不到将它们变成垂直线的方法。
我在 SO/SE 上找到了关于如何将垂直线添加到 plot() 的各种答案,但是我无法将我的数据转换为 plot() 可以使用的格式,因此我使用 plot_date ()。
样本数据:
date percentile
2012-05-30 3
2014-11-25 60
2012-06-15 38
2013-07-18 16
我绘制图表的代码如下:
x_data = data["date"]
y_data = data["percentile"]
plt.figure()
plt.plot()
#create a scatter chart of dates vs. percentile
plt.plot_date(x = x_data, y = y_data)
#now add a marker at prespecified dates - ideally this would be a vertical line
plt.plot_date(x = '2012-09-21', y = 0)
plt.savefig("percentile_plot.png")
plt.close()
很遗憾,我无法提供当前输出的图像,因为代码位于无法访问网络的终端上。
非常感谢任何帮助 - 在我如何提出问题方面也是如此,因为我对 SO / SE 很陌生。
谢谢。
【问题讨论】:
标签: date python-3.x matplotlib plot