| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| 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 |
|---|---|---|
| Expand Up | @@ -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. | ||
| Expand Down Expand Up | @@ -427,6 +440,32 @@ def create_artists(self, legend, orig_handle, | |
| return [legline] | ||
|
|
||
|
|
||
| class HandlerPatchCollection(HandlerPatch): | ||
| """ | ||
| Handler for `.PatchCollection` instances. | ||
| """ | ||
| 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 thread
Comment on lines
+457
to
+466
Copy link
Copy Markdown
Member
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. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityThis appears to be identical to HandlerPatch.create_artists, so can be left out.
Sorry, something went wrong.
All reactions
|
||
|
|
||
|
|
||
| class HandlerRegularPolyCollection(HandlerNpointsYoffsets): | ||
| r"""Handler for `.RegularPolyCollection`\s.""" | ||
|
|
||
| Expand Down Expand Up | @@ -775,31 +814,20 @@ class HandlerPolyCollection(HandlerBase): | |
| `~.Axes.stackplot`. | ||
| """ | ||
| def _update_prop(self, legend_handle, orig_handle): | ||
| 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 | ||
|
|
||
| # 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._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_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. | ||
|
|
||
| Expand Down | ||
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityWe should document that all properties are taken from the first patch.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.