Python 在Bokeh的条形图顶部添加错误条
我正在尝试创建顶部有错误条的条形图。我查看了以下内容以生成这样的视图。我的代码一直工作到我执行Python 在Bokeh的条形图顶部添加错误条,python,python-3.x,bokeh,Python,Python 3.x,Bokeh,我正在尝试创建顶部有错误条的条形图。我查看了以下内容以生成这样的视图。我的代码一直工作到我执行p.line(y\u err\u x,y\u err\u y,color=“black”)可能是由于x轴索引,因为我得到以下错误:无法获得未定义或空引用的属性“A” 什么是适当的用途?提前谢谢 from bokeh.io import show, output_notebook from bokeh.models import ColumnDataSource from bokeh.plotting i
p.line(y\u err\u x,y\u err\u y,color=“black”)
可能是由于x
轴索引,因为我得到以下错误:无法获得未定义或空引用的属性“A”
什么是适当的用途?提前谢谢
from bokeh.io import show, output_notebook
from bokeh.models import ColumnDataSource
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
output_notebook()
groups= ['A', 'B', 'C', 'D']
counts = [5, 3, 4, 2]
yerr = [1,2,3,4]
source = ColumnDataSource(data=dict(groups=groups, counts=counts))
p = figure(x_range=groups, plot_height=350, toolbar_location=None, title="Values")
p.vbar(x='groups', top='counts', width=0.9, source=source, legend="groups",
line_color='white', fill_color=factor_cmap('groups', palette=["#962980","#295f96","#29966c","#968529"],
factors=groups))
y_err_x = []
y_err_y = []
for px, py, err in zip(groups, counts, yerr):
y_err_x.append((px, px))
y_err_y.append((py - err, py + err))
p.line(y_err_x, y_err_y, color="black" )
p.xgrid.grid_line_color = None
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
show(p)
答案的技巧不再是必要的。错误注释现已内置到Bokeh中,请参阅文档: 及 下面是一个完整的示例:
from bokeh.io import show, output_file
from bokeh.models import ColumnDataSource, Whisker
from bokeh.plotting import figure
from bokeh.transform import factor_cmap
output_file("error.html")
groups= ['A', 'B', 'C', 'D']
counts = [5, 3, 4, 2]
error = [0.8, 0.4, 0.4, 0.3]
upper = [x+e for x,e in zip(counts, error) ]
lower = [x-e for x,e in zip(counts, error) ]
source = ColumnDataSource(data=dict(groups=groups, counts=counts, upper=upper, lower=lower))
p = figure(x_range=groups, plot_height=350, toolbar_location=None, title="Values", y_range=(0,7))
p.vbar(x='groups', top='counts', width=0.9, source=source, legend="groups",
line_color='white', fill_color=factor_cmap('groups', palette=["#962980","#295f96","#29966c","#968529"],
factors=groups))
p.add_layout(
Whisker(source=source, base="groups", upper="upper", lower="lower", level="overlay")
)
p.xgrid.grid_line_color = None
p.legend.orientation = "horizontal"
p.legend.location = "top_center"
show(p)