[英]How to plot a curve and error bars of the same color in holoviews with matplotlib?
我想使用matplotlib
后端在holoviews
繪制帶有誤差條的曲線。 我希望曲線和誤差條是相同的顏色,但沒有明確指定曲線的顏色。 即我可以很容易地做到這一點
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
要得到
但如果我給了mean_plot
並且還不知道它的顏色怎么辦? 我確定當前的選項必須存儲在實例的某個地方,但我不知道如何訪問它們。 我想做點什么
mean_color = mean_plot.<access_options_somehow>.color
err_plot = hv.ErrorBars((range(len(means)), means, errors)).opts(edgecolor=mean_color)
我沒有安裝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)
根據@ Sheldore的回答和我在那里的評論,這里有一種方法,首先將holoviews
元素渲染為matplotlib
圖,然后在那里找到顏色。 這不是很優雅,我認為必須有一個更好的方式,但它完成了工作。
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.