基于日期时间数据在matplotlib中使用axvline添加垂直线
Adding a verticle line using axvline in matplotlib based on datetime data
我正在尝试使用 axvline 将垂直线添加到使用 matplotlib 的折线图中,但 运行 出现了重复出现的错误。我一直在寻找解决方案并阅读此处的建议 How to draw vertical lines on a given plot in matplotlib but they do not look specifically at using datetime. A solution is discussed here matplotlib plot_date() add vertical line at specified date and a similar answer is given here How do you plot a vertical line on a time series plot in Pandas? which are helpful, but when I implement the advice (the advice is basically to add plt.axvline(dt.datetime(2020, 9, 21)) my line chart squashes all of the data into a single spot and adds a vertical line right at the end of the chart like so。
这是我的代码:
Australia_data.set_index('date')['new_cases'].plot()
sns.set(font_scale=1.4)
Australia_data.set_index('date')['new_cases'].plot(figsize=(12, 10), linewidth=2.5)
plt.xlabel("Date", labelpad=15)
plt.ylabel("New Cases", labelpad=15)
plt.title("Australia daily COVID-19 cases", y=1.02, fontsize=22)
plt.axvline(dt.datetime(2020, 9, 21))
真的不确定这里出了什么问题,任何建议将不胜感激!
您应该能够同时使用 pandas.Timestamp
和 Python datetime
对象作为 axvline
:
的 x 坐标
from datetime import datetime
from matplotlib import pyplot as plt
import pandas as pd
# dummy dataframe with a datetime index:
df = pd.DataFrame({'v': range(12)},
index=pd.date_range('2020', periods=12, freq='MS'))
df.plot()
# using a pandas timestamp:
plt.axvline(pd.Timestamp("2020-06-01"), color='r')
# using a datetime object:
plt.axvline(datetime(2020,8,1), color='g')
给出:
版本:Python3.8.7 x86-64,pandas1.2.1,matplotlib 3.3.3
我正在尝试使用 axvline 将垂直线添加到使用 matplotlib 的折线图中,但 运行 出现了重复出现的错误。我一直在寻找解决方案并阅读此处的建议 How to draw vertical lines on a given plot in matplotlib but they do not look specifically at using datetime. A solution is discussed here matplotlib plot_date() add vertical line at specified date and a similar answer is given here How do you plot a vertical line on a time series plot in Pandas? which are helpful, but when I implement the advice (the advice is basically to add plt.axvline(dt.datetime(2020, 9, 21)) my line chart squashes all of the data into a single spot and adds a vertical line right at the end of the chart like so。
这是我的代码:
Australia_data.set_index('date')['new_cases'].plot()
sns.set(font_scale=1.4)
Australia_data.set_index('date')['new_cases'].plot(figsize=(12, 10), linewidth=2.5)
plt.xlabel("Date", labelpad=15)
plt.ylabel("New Cases", labelpad=15)
plt.title("Australia daily COVID-19 cases", y=1.02, fontsize=22)
plt.axvline(dt.datetime(2020, 9, 21))
真的不确定这里出了什么问题,任何建议将不胜感激!
您应该能够同时使用 pandas.Timestamp
和 Python datetime
对象作为 axvline
:
from datetime import datetime
from matplotlib import pyplot as plt
import pandas as pd
# dummy dataframe with a datetime index:
df = pd.DataFrame({'v': range(12)},
index=pd.date_range('2020', periods=12, freq='MS'))
df.plot()
# using a pandas timestamp:
plt.axvline(pd.Timestamp("2020-06-01"), color='r')
# using a datetime object:
plt.axvline(datetime(2020,8,1), color='g')
给出:
版本:Python3.8.7 x86-64,pandas1.2.1,matplotlib 3.3.3