1

When running the below code, it displays a wrong plot.

from pandas_datareader import data
from datetime import datetime as dt
from bokeh.plotting import figure, show, output_file

st=dt(2016,3,1)
end=dt(2016,3,10)
df=data.DataReader(name="GOOG",data_source="yahoo",start=st,end=end)
p=figure(x_axis_type="datetime",height=300,width=1000)
p.title.text="CandleStick graph"
p.rect(df.index[df.Close > df.Open],(df.Close+df.Open)/2,12*60*60*1000,abs(df.Close-df.Open))
show(p)

1 Answers1

2

All the data columns need to be the same length, but you are passing one that is shorter than the others:

df.index[df.Close > df.Open]

Actually running your code, Bokeh even tells you exactly this:

BokehUserWarning: ColumnDataSource's columns must be of the same length. 
Current lengths: ('height', 9), ('x', 5), ('y', 9)

You are only passing 5 coordinates for x and 9 coordinates for all the others. The arguments all need to match up. You can either:

  • Not do the subsetting on df.index at all

  • Subset all the other arguments in the same way

(For future reference: you should always include any error or warning messages such as the one above in your SO questions—and, as @sjc mentioned, describe the problem in actual detail, not just state "is wrong")

bigreddot
  • 33,642
  • 5
  • 69
  • 122