Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw multiple line above candlestick chart?

I have a code to create OHLC chart from pylab_examples example code: finance_demo.py from http://matplotlib.org/examples/pylab_examples/finance_demo.html :

chart output

enter image description here

then i try to draw a horizontal line at 20 by date to date but i get an error with empty figure.

Here is code :

#!/usr/bin/env python
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter, WeekdayLocator,\
    DayLocator, MONDAY
from matplotlib.finance import quotes_historical_yahoo_ohlc, candlestick_ohlc


# (Year, month, day) tuples suffice as args for quotes_historical_yahoo
date1 = (2004, 2, 1)
date2 = (2004, 4, 12)


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_ohlc('INTC', date1, date2)
if len(quotes) == 0:
    raise SystemExit

fig, ax = plt.subplots()
fig.subplots_adjust(bottom=0.2)
ax.xaxis.set_major_locator(mondays)
ax.xaxis.set_minor_locator(alldays)
ax.xaxis.set_major_formatter(weekFormatter)
#ax.xaxis.set_minor_formatter(dayFormatter)

#plot_day_summary(ax, quotes, ticksize=3)
candlestick_ohlc(ax, quotes, width=0.6)

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

plt.show()

How can i add more line?

Thank you.

like image 925
YONG BAGJNS Avatar asked Oct 25 '25 08:10

YONG BAGJNS


1 Answers

You cannot use that axis again to draw something else which you have already used for drawing candlestick plot.

You need to have another axis ie add another subplot of the same region.

from matplotlib.finance import candlestick_ochl
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib.ticker as mticker

fig = plt.figure()
ax1 = fig.add_subplot(111) # in this function, first number 1 represent the height,
# second is for width and third is the plot number
ax2 = fig.add_subplot(111) # make sure both have same arguments for add_subplot function

ochl = [[mdates.date2num(datetime.date(2014, 12, 29)),20,30,35,15],
    [mdates.date2num(datetime.date(2015, 1, 30)),21,14,40,10],
    [mdates.date2num(datetime.date(2015, 2, 28)),23,29,45,15]]

# it is in the same expected order    date,open,high,low,close
candlestick_ochl(ax1, ochl, width=4, colorup='g', colordown='r', alpha=1)
ax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))
ax1.xaxis.set_major_locator(mticker.MaxNLocator(4))
ax1.grid(True)
ax2.plot([mdates.date2num(datetime.date(2014, 12, 29)),mdates.date2num(datetime.date(2015, 1, 29))],[20,30],color = 'b')
# make sure in the second plot, the xaxis is also like date type converted to number by date2num function
plt.show()

and the result for the above code

plot for the above code

enter image description here

like image 57
sheshant Avatar answered Oct 27 '25 23:10

sheshant



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!