0

enter image description here

The above image is what I want. Note the red annotation Big event happend with an arrow to the X-axis label 2017-12-08.

My code so far:

import matplotlib.pyplot as plt
import matplotlib.colors as colors
import numpy as np
import pandas as pd

if __name__ == '__main__':

    df = pd.DataFrame({
        'experiment': [1, 2, 3],
        'control': [2, 3, 4],
        'date': ['2017-12-08', '2017-12-09', '2017-12-10'],
    })

    df['date'] = pd.to_datetime(df['date'])

    df.plot(x='date', y=['experiment', 'control'])

    plt.show()

My result so far

enter image description here

Zizheng Wu
  • 646
  • 1
  • 12
  • 21

1 Answers1

0

Here's one way to use annotate (see docs for lots of cosmetic configuration options):

ax = df.plot(x='date', y=['experiment', 'control'])
ax.annotate('Big event happened', 
            xy=(df.date[1], 1), xytext=(3, 15),
            textcoords='offset points', 
            arrowprops=dict(facecolor='red', shrink=0.05))

plot

Matplotlib is smart enough to handle a datetime format for the x coordinate. You can also use date2num to convert to its internal numeric representation of date values, as shown in this answer.

andrew_reece
  • 20,390
  • 3
  • 33
  • 58