[英]How to add overlaying lines in matplotlib at specific points
I've created a simple line graph:我创建了一个简单的折线图:
x = df['dates']
y = df['score']
plt.plot(x, y, marker = 'o')
plt.show()
I have a second dateframe(df2) with a couple dates of interest我有第二个 dateframe(df2) 有几个感兴趣的日期
df2['dates_of_interest']
2021-07-15
2021-09-30
How can I overlay those two dates with two lines on the graph I've already created so it would look something like this:我怎样才能在我已经创建的图表上用两条线覆盖这两个日期,这样它看起来像这样:
You can use plt.axvline
function to add a vertical line.您可以使用plt.axvline
function 添加垂直线。
import matplotlib.pyplot as plt
import pandas as pd
from datetime import datetime
df = pd.DataFrame({'dates':[datetime(2021,m,1) for m in range(2, 13)],
'score':range(2, 13)})
plt.plot(df['dates'], df['score'], marker='o')
plt.xticks(rotation=45)
plt.axvline(datetime(2021,7,15), ls='--')
plt.axvline(datetime(2021,9,30), ls='--')
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.