Get color from legend for errorbar plots

Hi,

I?ve stumbled upon this very exact issue, for which there is no posted
answer:
https://stackoverflow.com/questions/26573864/matplotlib-get-errorbar-color-from-legend

Does anyone have an idea?

Regards,
Bruno

Le 22/05/2019 ? 20:34, Bruno Pagani a ?crit?:

Hi,

I?ve stumbled upon this very exact issue, for which there is no posted
answer:
python - matplotlib get errorbar color from legend - Stack Overflow

Does anyone have an idea?

Regards,
Bruno

As a side note, I have a workaround that rely on issuing a subsequent
plt.plot with the same data without errorbars, but if someone has a
better solution?

As Tom Caswell pointed out, you can use the legendHandles. Something like this may do what you want

plt.errorbar([1,2,3],[1,2,3],yerr=0.2,label='22')
plt.errorbar([3,1,2],[2,3,1],yerr=0.3,label='33')
z = plt.legend()
[lc.get_colors() for lc in z.legendHandles]

hth,
Scott

···

On May 22, 2019, at 2:34 PM, Bruno Pagani <bruno.pagani at astrophysics.eu> wrote:

Hi,
I?ve stumbled upon this very exact issue, for which there is no posted
answer:
python - matplotlib get errorbar color from legend - Stack Overflow
Does anyone have an idea?
Regards,
Bruno

Le 22/05/2019 ? 23:43, Scott Lasley a ?crit?:

As Tom Caswell pointed out, you can use the legendHandles. Something like this may do what you want

plt.errorbar([1,2,3],[1,2,3],yerr=0.2,label='22')
plt.errorbar([3,1,2],[2,3,1],yerr=0.3,label='33')
z = plt.legend()
[lc.get_colors() for lc in z.legendHandles]

hth,
Scott

Note that for some reason lc.get_colors() returns array of color in rgba
format, e.g. [[r g b a]]. So I had to do lc.get_colors()[0] instead, but
then it works fine.

Thanks for your help,
Bruno

Hey Bruno,

In this case, I believe `lc` is a LineCollection, so it's essentially a
sequence of N lines. In this case, it appear N = 1.
-p

···

On Thu, May 23, 2019 at 3:34 AM Bruno Pagani <bruno.pagani at astrophysics.eu> wrote:

Le 22/05/2019 ? 23:43, Scott Lasley a ?crit :
> As Tom Caswell pointed out, you can use the legendHandles. Something
like this may do what you want
>
> plt.errorbar([1,2,3],[1,2,3],yerr=0.2,label='22')
> plt.errorbar([3,1,2],[2,3,1],yerr=0.3,label='33')
> z = plt.legend()
> [lc.get_colors() for lc in z.legendHandles]
>
> hth,
> Scott

Note that for some reason lc.get_colors() returns array of color in rgba
format, e.g. [[r g b a]]. So I had to do lc.get_colors()[0] instead, but
then it works fine.

Thanks for your help,
Bruno

_______________________________________________
Matplotlib-users mailing list
Matplotlib-users at python.org
Matplotlib-users Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-users/attachments/20190523/18e84cd5/attachment.html&gt;

Le 23/05/2019 ? 15:28, Paul Hobson a ?crit?:

Hey Bruno,

In this case, I believe `lc` is a LineCollection, so it's essentially
a sequence of N lines. In this case, it appear N = 1.
-p

Makes sense, thx.

I?m posting an answer to the stackexchange question crediting all of you
for your pointers.

Regards,
Bruno