在散景中的条形图上添加错误条 [英] Adding error bars on top of bar charts in Bokeh

查看:122
本文介绍了在散景中的条形图上添加错误条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建在顶部带有错误条的条形图.我查看了以下 answer 生成这样的视图.我的代码可以正常工作,直到我执行p.line(y_err_x, y_err_y, color="black" )大概是由于x轴分度,因为出现以下错误:Unable to get property 'A' of undefined or null reference

I am trying to create bar chart with error bars on top. I looked at the following answer to generate such view. My code works until I do p.line(y_err_x, y_err_y, color="black" ) presumably due to x axis indexing as I get the following error: Unable to get property 'A' of undefined or null reference

什么是适当的用途?预先感谢!

What is the appropriate use? Thanks in advance!

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中,请参阅文档:

The technique of that answer is no longer necessary. Error annotations are now built into Bokeh, see the documentation:

https://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#whiskers

https://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#bands

这是一个完整的示例:

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)

这篇关于在散景中的条形图上添加错误条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆