mhicks
mhicks

Reputation: 13

Errorbar plot with multiple y-axes produces ValueError

I'm trying to create an errorbar plot with multiple y-axes using as a template the plot with multiple axes from here. If I alter only this line of the template:

p1, = host.plot([0, 1, 2], [0, 1, 2], "b-", label="Density")

to this:

p1, = host.errorbar([0, 1, 2], [0, 1, 2], yerr=[0.5, 0.5, 0.5], fmt='b-', label="Density")

I get as output:

  1. The errorbar plot with multiple y-axes but only the p1 data are displayed.
  2. A 'ValueError: too many values to unpack'.

Could someone help explain why this is the case? Thanks in advance.

Upvotes: 1

Views: 146

Answers (1)

cge
cge

Reputation: 9890

errorbar and plot return different things. plot returns a list of lines, but in this case, the code is assuming that there's only a single line returned, and so p1, and so on takes that out of the list.

errorbar instead returns a Container object, and the container contains multiple lines. You can't use p1, for errorbar's return. You'll need instead to deal with it differently.

p1 is used for two different purposes later in the code: to set the color, and to set the label. As it turns out, get_label is a method of the Container. get_color isn't, but it is a method of children of the container, and the should be the same color. So change the code to just assign the container to p1, and use the color of one of its children. For example:

p1 = host.errorbar([0, 1, 2], [0, 1, 2], yerr=[0.5, 0.5, 0.5], 
                   fmt='b-', label="Density")

Then:

host.yaxis.label.set_color(p1.get_children()[0].get_color())

and

host.tick_params(axis='y', colors=p1.get_children()[0].get_color(), **tkw)

The legend-setting code doesn't need to be changed at all.

Upvotes: 1

Related Questions