python - Creating a line graph with ggplot library which plots two lines (the high and low of stock prices) on the same graph -
i can plot closing prices using below syntax:
gg = ggplot(stock_prices.ix['2013-01-01': '2013-12-31'], aes('date', 'close')) + geom_line()
however, don't know how plot "high" , "low" prices on same plot, ideas?
the standard practice of plotting "high" , "lows" (together "open" , "close) in finance candlestick plotting. following code should started in charting candlesticks:
step 1. imports.
import datetime dt import matplotlib.pyplot plt import matplotlib.dates mdates matplotlib.finance import candlestick_ohlc import pandas pd pandas_datareader import data
step 2. downloads.
start = dt.datetime(2015,1,1) spy = data.datareader('^gspc', 'yahoo', start)
you can skip step if have data in ohlc format.
step 3. prepare data.
at point have data in form:
spy.head() open high low close volume adj close date 2015-01-02 2058.899902 2072.360107 2046.040039 2058.199951 2708700000 2058.199951 2015-01-05 2054.439941 2054.439941 2017.339966 2020.579956 3799120000 2020.579956 2015-01-06 2022.150024 2030.250000 1992.439941 2002.609985 4460110000 2002.609985 2015-01-07 2005.550049 2029.609985 2005.550049 2025.900024 3805480000 2025.900024 2015-01-08 2030.609985 2064.080078 2030.609985 2062.139893 3934010000 2062.139893
to move further, need have index in separate column number of dates, rather timestamp, achieved by:
spy['date2'] = spy.index.map(lambda x: mdates.date2num(x))
step 4. candlestick plotting.
now have data in place can plot:
fig, ax = plt.subplots() ax.xaxis_date() ax.xaxis.set_major_formatter(mdates.dateformatter("%y-%m-%d")) plt.xlabel("date") plt.ylabel("price") plt.title("sp500") candlestick_ohlc(ax, spy[['date2', 'open', 'high', 'low','close']].values, width=.6, colorup='g', alpha =.4);
edit
should still wish have 2 separate lines high , low following line of code that:
spy[['high','low']].plot();
Comments
Post a Comment