-
-
Notifications
You must be signed in to change notification settings - Fork 7.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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FarukFS
wants to merge
1
commit into
matplotlib:main
Choose a base branch
from
FarukFS:my-feature
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
""" | ||
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): | ||
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. | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should document that all properties are taken from the first patch.