Moondra
Moondra

Reputation: 4511

Annotating a graph with certain values of another series (Index is datetime)

df =

date          Close     Bullish
2010-04-07    2.02        0
2010-04-08    2.03        0
2010-04-09    2.05        1
2010-04-12    2.16        1
2010-04-13    2.32        1





fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(df.Close, '-')
ax.annotate('Bullish', xy= , xytext=(1, 1.),
        arrowprops=dict(facecolor='black', shrink=),
        )

So I'm plotting the closing price of a stock, and I want to annotate the line graph with the word 'Bullish', wherever there is a 1 value in the 'Bullish' column.

I'm not too familiar with annotations so I'm not getting too far.

Thanks.

Upvotes: 2

Views: 830

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

I think you were missing using matplotlib.dates with your annotations. See this SO post.

import matplotlib.pyplot as plt
import matplotlib.dates as mdates
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(df.Close, '-')

for i in df.query('Bullish == 1').iterrows():
    ax.annotate('Bullish',xy=(mdates.date2num(i[0]),i[1][0]), xytext=(15,15), textcoords='offset points',
        arrowprops=dict(facecolor='black'))

plt.xticks(rotation=45)

enter image description here

Upvotes: 3

Related Questions