Issue
I import a simple data frame from Excel. Seven dates, 12/2021 - 12/2027, each date a year apart, each associated with a number. I want to add an annotation (a vertical line separating the actual historical value from forecasted numbers), and text making this clear.
df.plot()
#annotation
plt.axvline(pd.to_datetime('2023-6-01'))
plt.text(pd.to_datetime('2022-06-01'), 20**6, 'actual')
plt.text(pd.to_datetime('2023-06-01'), 20**6, 'forecast')
plt.show()
The system rounds my placements to the nearest year-end. So with the code above, all three placements are set six months earlier than I want them to be.
How can I get the line and text to appear in the specified months?
Solution
The problem was fixed by using a subplots. So replace the code above with
figure, ax = plt.subplots() # need both of those; subplots is a tuple
ax.plot(df)
#annotation
ax.axvline(pd.to_datetime('2022-6-01'))
ax.text(pd.to_datetime('2021-06-01'), 20**6, 'actual')
ax.text(pd.to_datetime('2022-06-01'), 20**6, 'forecast')
plt.show()
and the annotations can be placed freely.
Answered By - Michael Stern
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.