如何将误差线添加到 python 中的散景图?

How do you add error bars to Bokeh plots in python?

我已经使用 Bokeh 成功地绘制了几个数据集和拟合函数,但是我真的需要在图表中添加误差线,我该怎么做呢?

编辑:现在内置于 Bokeh 中,请参阅文档:

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

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

有关完整示例,请参阅


也许有点晚了,但我今天也想这样做。

很遗憾bokeh本身不提供这个功能

import numpy as np
from bokeh.plotting import figure, show, output_file

# some pseudo data
xs = np.linspace(0, 2*np.pi, 25)
yerrs = np.random.uniform(0.1, 0.3, xs.shape)
ys = np.sin(xs) + np.random.normal(0, yerrs, xs.shape)

output_file('bokeh_errorbars.html')

# plot the points
p = figure(title='errorbars with bokeh', width=800, height=400)

p.xaxis.axis_label = 'x'
p.yaxis.axis_label = 'y'


p.circle(xs, ys, color='red', size=5, line_alpha=0)


# create the coordinates for the errorbars
err_xs = []
err_ys = []

for x, y, yerr in zip(xs, ys, yerrs):
    err_xs.append((x, x))
    err_ys.append((y - yerr, y + yerr))

# plot them
p.multi_line(err_xs, err_ys, color='red')

show(p)

这是结果:

有人可能想像这样将其用作函数:

def errorbar(fig, x, y, xerr=None, yerr=None, color='red', 
             point_kwargs={}, error_kwargs={}):

  fig.circle(x, y, color=color, **point_kwargs)

  if xerr:
      x_err_x = []
      x_err_y = []
      for px, py, err in zip(x, y, xerr):
          x_err_x.append((px - err, px + err))
          x_err_y.append((py, py))
      fig.multi_line(x_err_x, x_err_y, color=color, **error_kwargs)

  if yerr:
      y_err_x = []
      y_err_y = []
      for px, py, err in zip(x, y, yerr):
          y_err_x.append((px, px))
          y_err_y.append((py - err, py + err))
      fig.multi_line(y_err_x, y_err_y, color=color, **error_kwargs)