我想为我的点线图绘制误差线(std(。我没有找到如何做到这一点的正确答案,但解决方案之一是使用胡须。https://docs.bokeh.org/en/latest/docs/user_guide/annotations.html#whiskers
我为我工作得很好,但我有一个问题,如何改变胡须头的头部颜色。
我使用线条颜色属性来更改线条的颜色:https://docs.bokeh.org/en/latest/docs/reference/models/annotations.html#bokeh.models.annotations.Whisker
但它没有改变头部的颜色(水平线(。它仍然是黑色的。有没有办法做到这一点?或者有更好的方法如何在线条的颜色中绘制误差线?
散景示例代码,带有红线和黑头的误差线:
from bokeh.models import ColumnDataSource, Whisker
from bokeh.plotting import figure, show
from bokeh.sampledata.autompg import autompg as df
colors = ["red", "olive", "darkred", "goldenrod", "skyblue", "orange", "salmon"]
p = figure(plot_width=600, plot_height=300, title="Years vs mpg with Quartile Ranges")
base, lower, upper = [], [], []
for i, year in enumerate(list(df.yr.unique())):
year_mpgs = df[df['yr'] == year]['mpg']
mpgs_mean = year_mpgs.mean()
mpgs_std = year_mpgs.std()
lower.append(mpgs_mean - mpgs_std)
upper.append(mpgs_mean + mpgs_std)
base.append(year)
source_error = ColumnDataSource(data=dict(base=base, lower=lower, upper=upper))
p.add_layout(
Whisker(source=source_error, base="base", upper="upper", lower="lower", line_color='red')
)
for i, year in enumerate(list(df.yr.unique())):
y = df[df['yr'] == year]['mpg']
color = colors[i % len(colors)]
p.circle(x=year, y=y, color=color)
show(p)
晶须的末端可以通过upper_head
和lower_head
属性进行配置,这些属性是具有自己line_color
等的ArrowHead
对象。配置它们的最简单方法可能是这样的:
w = Whisker(source=source_error, base="base", upper="upper", lower="lower",
line_color='red')
w.upper_head.line_color = 'red'
w.lower_head.line_color = 'red'
p.add_layout(w)
虽然也可以将显式配置的TeeHead
传递给Whisker
,如果你真的想在一个表达式中做所有事情:
Whisker(...,
upper_head=TeeHead(line_color='red'),
lower_head=TeeHead(line_color='red'))