Bokeh绘图错误。

huangapple go评论70阅读模式
英文:

Wrong plotting in bokeh

问题

当运行以下代码时,它显示了一个错误的图表。

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图"
p.rect(df.index[df.Close > df.Open],(df.Close+df.Open)/2,12*60*60*1000,abs(df.Close-df.Open))
show(p)

请注意,我已经将代码部分保持原样,只进行了翻译。

英文:

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

得分: 2

所有数据列都需要具有相同的长度,但您正在传递一个比其他列更短的列:

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

实际运行您的代码,Bokeh甚至告诉您确切的问题:

BokehUserWarning: ColumnDataSource的列必须具有相同的长度当前长度为('height', 9)('x', 5)('y', 9)

您只为x传递了5个坐标点,而对于所有其他列却传递了9个坐标点。所有参数都需要匹配。您可以选择:

  • 完全不对df.index进行子集化

  • 以相同的方式对所有其他参数进行子集化

(供以后参考:您应该始终在您的问题中包含任何错误或警告消息,例如上面的消息,并且如@sac所提到的,要详细描述问题,而不仅仅是说"是错误的"。)

英文:

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")

huangapple
  • 本文由 发表于 2020年1月7日 02:31:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定