[Matplotlib-users] Get color from legend for errorbar plots

Bruno Pagani bruno.pagani at astrophysics.eu
Thu May 23 06:34:25 EDT 2019

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,

More information about the Matplotlib-users mailing list