Question

I have script which draw candlestick chart. I added two annotations to this script. One is "Last data" which show last candle and the second annotations should show point in the future. But for some reason it is not visible on the picture. How can I make it visible? I was trying to change chart width and plt.axis values but didn't find solution.

#!/usr/bin/env python
import matplotlib.pyplot as plt
from matplotlib.dates import  DateFormatter, WeekdayLocator, HourLocator, \
     DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo, candlestick,\
     plot_day_summary, candlestick2

from matplotlib.patches import Ellipse, Circle
el = Ellipse((2, -1), 0.5, 0.5)

# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = ( 2013, 11, 10)
date2 = ( 2013, 12, 29)


mondays = WeekdayLocator(MONDAY)        # major ticks on the mondays
alldays    = DayLocator()              # minor ticks on the days
weekFormatter = DateFormatter('%b %d')  # e.g., Jan 12
dayFormatter = DateFormatter('%d')      # e.g., 12

quotes = quotes_historical_yahoo('INTC', date1, date2)

if len(quotes) == 0:
    raise SystemExit

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)

candlestick(ax, quotes, width=0.6)

ax.xaxis_date()
plt.setp( plt.gca().get_xticklabels(), rotation=45, horizontalalignment='right')

dt = quotes[0][0]

ax.annotate(
                'Last data',
                xy=(dt+16, 24),
                xytext=(dt+3, 25),
                arrowprops=dict(arrowstyle='simple', fc="0.6", ec="none", patchB=el, connectionstyle="arc3,rad=0.3", facecolor='black')
        )

ax.annotate(
                'Future',
                xy=(dt+19, 24.5),
                xytext=(dt+2, 23.7),
                arrowprops=dict(arrowstyle='simple', fc="0.6", ec="none", patchB=el, connectionstyle="arc3,rad=0.3", facecolor='black')
        )

plt.savefig('test.png')

annotations

Was it helpful?

Solution

You just need to change the xlimit on your graph:

ax = plt.gca()
ax.set_xlim([start_data, end_date])
plt.draw()

OTHER TIPS

The code as (was) written works for me on OS X, Python 2.7.5.

Both the 'Future' and 'Last data' arcs are drawn and labelled.

If you count the candlesticks, there are only 13 - which you also see if you add a print statement:

quotes = quotes_historical_yahoo('INTC', date1, date2)
print len(quotes)

As your code indicates, everything past the 28th Nov is a 'future date' so there is no data for it -- thus the return from quotes_historical_yahoo() only has 13 days of quotes (plus apparently some gaps for the weekends).

You can't annotate points past the 28th November because they aren't in the plotted data, so the call fails.

This code can be added as workaround:

line = Line2D(
        xdata=(dt+19, dt+19),
        ydata=(24.5, 24.5), )

ax.add_line(line) ax.autoscale_view()

Annotation can be created now:

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top