Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib Patches - Extracting the containing information

so I am working with a Python package (Pycalphad) which uses matplotlib.patches for plotting the legend of diagrams. These patches contain the colors and respective labels.

In the package, the entries are defined as (mpatches.Patch(color=colorlist[phase], label=phase)), with phase being a changing variable.

If I wanna read one of the legend entries, I only get back <matplotlib.patches.Patch at 0x14e5373f108>.
I would prefer not to change the code of the package and rather work with what I got. So is there a way to extract the information contained in the Patches and getting the colors and labels used in the definition of the patch?
Help would be highly appreciated!

like image 905
Jusles Avatar asked Sep 12 '26 09:09

Jusles


1 Answers

You should use the get_... methods of Patch to access the properties you want:

from matplotlib import patches

my_patch = patches.Patch(color='red', label='my_patch')

my_patch.get_fc()  # this is the facecolor
Out[4]: (1.0, 0.0, 0.0, 1.0)

my_patch.get_ec()  # this is the edgecolor
Out[5]: (1.0, 0.0, 0.0, 1.0)

my_patch.get_label()  # and the label
Out[6]: 'my_patch'

So in this example my_patch is the object that gives <matplotlib.patches.Patch at 0x7fea0290d518> if I simply write my_patch in the console.

like image 124
jojo Avatar answered Sep 13 '26 23:09

jojo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!