简体   繁体   English

如何使用matplotlib在全息视图中绘制相同颜色的曲线和误差条?

[英]How to plot a curve and error bars of the same color in holoviews with matplotlib?

I want to plot a curve with error bars in holoviews using the matplotlib backend. 我想使用matplotlib后端在holoviews绘制带有误差条的曲线。 I would like the curve and the error bars to be the same color, but without explicitly specifying the color for the curve. 我希望曲线和误差条是相同的颜色,但没有明确指定曲线的颜色。 Ie I can easily do this 即我可以很容易地做到这一点

import holoviews as hv
hv.extension("matplotlib")

means = [1, 4, 2, 3]
errors = [0.3, 0.5, 0.2, 0.1]
color = "green"
mean_plot = hv.Curve(means).opts(color=color)
err_plot = hv.ErrorBars((range(len(means)), means, errors)).opts(edgecolor=color)
mean_plot * err_plot

to get 要得到

在此输入图像描述

but what if I was given mean_plot and didn't already know its color? 但如果我给了mean_plot并且还不知道它的颜色怎么办? I'm sure the current options must be stored somewhere on the instance, but I don't know how to access them. 我确定当前的选项必须存储在实例的某个地方,但我不知道如何访问它们。 I'd like to do something like 我想做点什么

mean_color = mean_plot.<access_options_somehow>.color
err_plot = hv.ErrorBars((range(len(means)), means, errors)).opts(edgecolor=mean_color)

I do not have holoviews installed but since it is using matplotlib , you can try the generic solution to extract the color of the line and then use it to plot the error bars 我没有安装holoviews但由于它使用matplotlib ,你可以尝试通用的解决方案来提取线的颜色,然后用它来绘制误差线

mean_plot = hv.Curve(means) # Don't specify any color here
mean_color = mean_plot[0].get_color() # Extract the default color
err_plot = hv.ErrorBars((range(len(means)), means, errors)).opts(edgecolor=mean_color)

Based on @Sheldore's answer and my comment there, here's an approach that first renders the holoviews element to a matplotlib figure and then finds the color there. 根据@ Sheldore的回答和我在那里的评论,这里有一种方法,首先将holoviews元素渲染为matplotlib图,然后在那里找到颜色。 This isn't very elegant, and I think there must be a nicer way, but it gets the job done. 这不是很优雅,我认为必须有一个更好的方式,但它完成了工作。

import holoviews as hv
hv.extension("matplotlib")

means = [1, 4, 2, 3]
errors = [0.3, 0.5, 0.2, 0.1]
color = "green"
mean_plot = hv.Curve(means).opts(color=color)

fig = hv.render(mean_plot)
ax = fig.axes[0]
line = ax.get_lines()[0]
mean_color = line.get_color()

err_plot = hv.ErrorBars((range(len(means)), means, errors)).opts(edgecolor=mean_color)
mean_plot * err_plot

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM