matplotlib Patches - Extracting the containing information

Viewed 172

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!

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.

Related