Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Created handler for PatchCollection#24028
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
base:main
Are you sure you want to change the base?
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
Legend handler for PatchCollection objects | ||
------------------------------------------ | ||
PatchCollection objects are now supported in legends. The feature can be used as follows: | ||
.. plot:: | ||
:include-source: true | ||
import matplotlib.pyplot as plt | ||
from matplotlib.collections import PatchCollection | ||
from matplotlib.patches import Polygon | ||
fig, axs = plt.subplots() | ||
p1, p2 = Polygon([[0, 0], [100, 100], [200, 0]]), Polygon([[400, 0], [500, 100], [600, 0]]) | ||
p3, p4 = Polygon([[700, 0], [800, 100], [900, 0]]), Polygon([[1000, 0], [1100, 100], [1200, 0]]) | ||
p = PatchCollection([p1, p2], label="a", facecolors='red', edgecolors='black') | ||
p2 = PatchCollection([p3, p4], label="ab", color='green') | ||
axs.add_collection(p, autolim=True) | ||
axs.add_collection(p2, autolim=True) | ||
axs.set_xlim(right=1200) | ||
axs.set_ylim(top=100) | ||
axs.legend() | ||
plt.show() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -43,6 +43,19 @@ def update_from_first_child(tgt, src): | ||
tgt.update_from(first_child) | ||
def _first_color(colors): | ||
if colors.size == 0: | ||
return (0, 0, 0, 0) | ||
return tuple(colors[0]) | ||
def _get_first(prop_array): | ||
if len(prop_array): | ||
return prop_array[0] | ||
else: | ||
return None | ||
class HandlerBase: | ||
""" | ||
A base class for default legend handlers. | ||
@@ -427,6 +440,32 @@ def create_artists(self, legend, orig_handle, | ||
return [legline] | ||
class HandlerPatchCollection(HandlerPatch): | ||
""" | ||
Handler for `.PatchCollection` instances. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. We should document that all properties are taken from the first patch. | ||
""" | ||
def _default_update_prop(self, legend_handle, orig_handle): | ||
lw = _get_first(orig_handle.get_linewidths()) | ||
dashes = _get_first(orig_handle._us_linestyles) | ||
facecolor = _first_color(orig_handle.get_facecolor()) | ||
edgecolor = _first_color(orig_handle.get_edgecolor()) | ||
legend_handle.set_facecolor(facecolor) | ||
legend_handle.set_edgecolor(edgecolor) | ||
legend_handle.set_linestyle(dashes) | ||
legend_handle.set_linewidth(lw) | ||
def create_artists(self, legend, orig_handle, | ||
xdescent, ydescent, width, height, fontsize, trans): | ||
p = self._create_patch(legend, orig_handle, | ||
xdescent, ydescent, width, height, fontsize) | ||
self.update_prop(p, orig_handle, legend) | ||
p.set_transform(trans) | ||
return [p] | ||
Comment on lines +457 to +466 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This appears to be identical to | ||
class HandlerRegularPolyCollection(HandlerNpointsYoffsets): | ||
r"""Handler for `.RegularPolyCollection`\s.""" | ||
@@ -775,31 +814,20 @@ class HandlerPolyCollection(HandlerBase): | ||
`~.Axes.stackplot`. | ||
""" | ||
def _update_prop(self, legend_handle, orig_handle): | ||
# orig_handle is a PolyCollection and legend_handle is a Patch. | ||
# Directly set Patch color attributes (must be RGBA tuples). | ||
legend_handle._facecolor =_first_color(orig_handle.get_facecolor()) | ||
legend_handle._edgecolor =_first_color(orig_handle.get_edgecolor()) | ||
legend_handle._original_facecolor = orig_handle._original_facecolor | ||
legend_handle._original_edgecolor = orig_handle._original_edgecolor | ||
legend_handle._fill = orig_handle.get_fill() | ||
legend_handle._hatch = orig_handle.get_hatch() | ||
# Hatch color is anomalous in having no getters and setters. | ||
legend_handle._hatch_color = orig_handle._hatch_color | ||
# Setters are fine for the remaining attributes. | ||
legend_handle.set_linewidth(_get_first(orig_handle.get_linewidths())) | ||
legend_handle.set_linestyle(_get_first(orig_handle.get_linestyles())) | ||
legend_handle.set_transform(_get_first(orig_handle.get_transforms())) | ||
legend_handle.set_figure(orig_handle.get_figure()) | ||
# Alpha is already taken into account by the color attributes. | ||
Uh oh!
There was an error while loading.Please reload this page.