0

enter image description hereI am adding an axv line in my python plot and I would like to add text on it. I added to it correctly but the text is overlapping with the axv line. So, I would like to move it a little backward or forward. But the problem is I am dealing with dates on x-axis and I couldn't just add some integer values on the x-axis. Please see my codes below and my resulting image. enter image description here

monthly_death = coup.groupby(pd.Grouper(key = "event_date", freq = "1M"))['fatalities'].sum()

ax = monthly_death.plot(kind = 'line', title = 'testing', figsize=(10, 6))

plt.axvline(x= '2021-09-01', color = 'red', ls = '--')
plt.text ('2021-09-01', 250, 'testing', rotation = 90)

plt.show()
Mr. T
  • 11,960
  • 10
  • 32
  • 54
Neo
  • 25
  • 4

1 Answers1

2

You can convert the datestring into the format that matplotlib uses internally and add some offset:

import pandas as pd 
import matplotlib.pyplot as plt
from matplotlib.dates import datestr2num

#sample data
import numpy as np
n = 50
rng = np.random.default_rng(123)
date_range = pd.date_range("2021-01-01", periods=n, freq="3D")
val = rng.integers(0, 1000, size=n)
df = pd.DataFrame({"Dates": date_range, "Values": val})
df = df.set_index("Dates")


ax = df.plot(kind = 'line', title = 'testing', figsize=(10, 6))
d = '2021-04-18'
ax.axvline(x=d, color = 'red', ls = '--')
#convert datestring to number internally used by matplotlib and add an offset value
ax.text(datestr2num(d)+1, 250, 'testing', rotation = 90)

plt.show()

Sample output: enter image description here

You should also stick to OOP when starting to use it. You can read about the differences between pyplot and OOP here.

Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • Thanks a lot. But I am getting this following error. ValueError: Image size of 466793x291 pixels is too large. It must be less than 2^16 in each direction.
    – Neo Apr 16 '22 at 16:07
  • I wrote basically the same code like you did. ```from matplotlib.dates import datestr2num monthly_death = coup.groupby(pd.Grouper(key = "event_date", freq = "1M"))['fatalities'].sum() d = '2021-04-18' ax = monthly_death.plot(kind = 'line', title = 'Deaths due to political violence') ax.axvline(d, color = 'red', ls = '--') ax.text(datestr2num(d)+1, 250, 'testing', rotation = 90) plt.show()``` – Neo Apr 16 '22 at 16:08
  • As I obviously cannot reproduce your problem, there is nothing I can contribute. – Mr. T Apr 16 '22 at 16:14
  • I found [this thread](https://stackoverflow.com/q/51980366/8881141), though. Seemingly, your datestring is not correctly interpreted. – Mr. T Apr 16 '22 at 16:20
  • I think the time string is parsed differently from the format produced by datestr2num. That's why it must be getting a value outside the figure. When I first processed the data, I used pd.to_datetime(df['event_date']) to ensure it has the correct type. Then, I used groupby and Grouper to group the data by months. I think it's probably because of Grouper since they are producing only first day of the month etc. – Neo Apr 16 '22 at 16:39