散景图没有显示任何内容

时间:2018-04-23 04:39:18

标签: pandas bokeh

我编写了一个Python程序,用于根据Google的财务数据绘制烛台图。但是我无法获得图表(没有显示任何内容)。我认为问题在于X坐标,即df.index。有人可以帮我解决这个问题吗?

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)

1 个答案:

答案 0 :(得分:1)

问题是df.index不是DateTimeIndex。它是由字符串组成的常规索引:

df.index
  

指数([&#39; 2016-03-01&#39;,&#39; 2016-03-02&#39;,&#39; 2016-03-03&#39;,&#39; 2016 -03-04&#39 ;,   &#39; 2016年3月7日&#39 ;,              &#39; 2016-03-08&#39;,&#39; 2016-03-09&#39;,&#39; 2016-03-10&#39;],             dtype =&#39; object&#39;,name =&#39; date&#39;)

因此,在指定x_axis_type='datetime'后,它不是散景所期望的。解决方案是将索引转换为DateTimeIndex,如下所示:

df.index = pd.to_datetime(df.index)
df.index
  

DatetimeIndex([&#39; 2016-03-01&#39;,&#39; 2016-03-02&#39;,&#39; 2016-03-03&#39;,&#39; 2016 -03-04&#39 ;,                      &#39; 2016-03-07&#39;,&#39; 2016-03-08&#39;,&#39; 2016-03-09&#39;,&#39; 2016-03-10&#39 ],                     dtype =&#39; datetime64 [ns]&#39;,name =&#39; date&#39;,freq = None)

然后,您可以正确显示您的情节!

相关问题