0

I want to plot a curve with error bars in holoviews using the matplotlib backend. I would like the curve and the error bars to be the same color, but without explicitly specifying the color for the curve. I.e. 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

enter image description here

but what if I was given mean_plot and didn't already know its color? 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)
Nathan
  • 9,651
  • 4
  • 45
  • 65

2 Answers2

0

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

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
  • 37,862
  • 7
  • 57
  • 71
  • This won't work with `holoviews`. Indexing into `mean_plot` gives the data used for the plot (so, in my example, `mean_plot[0]` is `1`). I haven't used it before, so I'd have to search the docs for how, but I believe there is a way to get the `matplotlib` figure from the `holoviews` element; it's possible that's the only way, though I'd prefer a `holoviews`-y solution if there is one. – Nathan May 16 '19 at 17:07
0

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. 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
Nathan
  • 9,651
  • 4
  • 45
  • 65