Get color from legend for errorbar plots

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Get color from legend for errorbar plots

Bruno Pagani
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

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Get color from legend for errorbar plots

Bruno Pagani
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:
> https://stackoverflow.com/questions/26573864/matplotlib-get-errorbar-color-from-legend
>
> 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…

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Get color from legend for errorbar plots

matplotlib - users mailing list
In reply to this post by Bruno Pagani
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 <[hidden email]> wrote:
>
> 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
_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Get color from legend for errorbar plots

Bruno Pagani
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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Get color from legend for errorbar plots

Paul Hobson-2
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 <[hidden email]> 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
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Get color from legend for errorbar plots

Bruno Pagani
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

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users