Reputation: 29
I wrote a Python program for plotting a candlestick graph based on financial data of Google. But I'm unable to get the graph (nothing is displayed). I think the problem is with the X coordinate i.e. df.index
. Can someone help me to solve this issue?
from pandas_datareader import data
import datetime
from bokeh.plotting import figure, show, output_file
start=datetime.datetime(2016,3,1)
end=datetime.datetime(2016,3,10)
df=data.DataReader(name="GOOG",data_source="iex",start=start,end=end)
def inc_dec(c,o):
if c>o:
value="increase"
elif c<o:
value="decrease"
else:
value="Equal"
return value
df["Status"]=[inc_dec(c,o) for c,o in zip(df.close,df.open)]
df["Middle"]=(df.close+df.open)/2
df["Height"]=abs(df.close-df.open)
p=figure(x_axis_type='datetime',width=1000, height=300)
p.title.text="Candlestick Chart"
p.segment(df.index,df.low,df.index,df.high,color="Black")
output_file("CS.html")
show(p)
Upvotes: 1
Views: 732
Reputation: 2307
The problem is that df.index
is not a DateTimeIndex. It is a regular index that consists of strings:
df.index
Index(['2016-03-01', '2016-03-02', '2016-03-03', '2016-03-04', '2016-03-07', '2016-03-08', '2016-03-09', '2016-03-10'], dtype='object', name='date')
Thus, it is not what bokeh expects after you specify x_axis_type='datetime'
. The solution is to convert the index to a DateTimeIndex like so:
df.index = pd.to_datetime(df.index)
df.index
DatetimeIndex(['2016-03-01', '2016-03-02', '2016-03-03', '2016-03-04', '2016-03-07', '2016-03-08', '2016-03-09', '2016-03-10'], dtype='datetime64[ns]', name='date', freq=None)
Then, you can properly display your plot!
Upvotes: 1