Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Add HandlerPatchCollection to support legends for PatchCollection #29952

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

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions lib/matplotlib/legend_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,3 +810,42 @@
self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
return [p]

Check failure on line 813 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Blank line contains whitespace Raw Output: message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:813 column:1} end:{line:813 column:5}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W293" url:"https://docs.astral.sh/ruff/rules/blank-line-with-whitespace"} suggestions:{range:{start:{line:813 column:1} end:{line:813 column:5}}}
from matplotlib.legend_handler import HandlerPatchCollection

Check failure on line 814 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Module level import not at top of file Raw Output: message:"Module level import not at top of file" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:814 column:1} end:{line:814 column:61}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E402" url:"https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file"}

Check warning on line 814 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L814

Added line #L814 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This module is legend_handler, so you do not need to import from it.


_default_handler_map[PatchCollection] = HandlerPatchCollection()

Check failure on line 816 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `_default_handler_map` Raw Output: message:"Undefined name `_default_handler_map`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:816 column:1} end:{line:816 column:21}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check failure on line 816 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Undefined name `PatchCollection` Raw Output: message:"Undefined name `PatchCollection`" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:816 column:22} end:{line:816 column:37}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F821" url:"https://docs.astral.sh/ruff/rules/undefined-name"}

Check warning on line 816 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L816

Added line #L816 was not covered by tests


from matplotlib.patches import Patch

Check failure on line 819 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Module level import not at top of file Raw Output: message:"Module level import not at top of file" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:819 column:1} end:{line:819 column:37}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E402" url:"https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file"}
from matplotlib.collections import PatchCollection

Check failure on line 820 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Module level import not at top of file Raw Output: message:"Module level import not at top of file" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:820 column:1} end:{line:820 column:51}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E402" url:"https://docs.astral.sh/ruff/rules/module-import-not-at-top-of-file"}

Check failure on line 820 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 `matplotlib.collections.PatchCollection` imported but unused Raw Output: message:"`matplotlib.collections.PatchCollection` imported but unused" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:820 column:36} end:{line:820 column:51}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"F401" url:"https://docs.astral.sh/ruff/rules/unused-import"} suggestions:{range:{start:{line:820 column:1} end:{line:821 column:1}}}

Check warning on line 820 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L819-L820

Added lines #L819 - L820 were not covered by tests

class HandlerPatchCollection(HandlerPatch):

Check failure on line 822 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 Expected 2 blank lines, found 1 Raw Output: message:"Expected 2 blank lines, found 1" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:822 column:1} end:{line:822 column:6}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"E302" url:"https://docs.astral.sh/ruff/rules/blank-lines-top-level"} suggestions:{range:{start:{line:821 column:1} end:{line:822 column:1}} text:"\n\n"}

Check warning on line 822 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L822

Added line #L822 was not covered by tests
"""
Handler for PatchCollection, e.g. from matplotlib.collections.
Uses the properties of the first patch in the collection.
"""

def create_artists(self, legend, orig_handle,

Check warning on line 828 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L828

Added line #L828 was not covered by tests
xdescent, ydescent, width, height, fontsize, trans):
p = Patch()

Check warning on line 830 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L830

Added line #L830 was not covered by tests

try:
fc = orig_handle.get_facecolor()[0]
ec = orig_handle.get_edgecolor()[0]
lw = orig_handle.get_linewidths()[0]
ls = orig_handle.get_linestyle()[0]
except IndexError:
fc, ec, lw, ls = 'gray', 'black', 1.0, 'solid'

Check warning on line 838 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L832-L838

Added lines #L832 - L838 were not covered by tests

p.set_facecolor(fc)
p.set_edgecolor(ec)
p.set_linewidth(lw)
p.set_linestyle(ls)

Check warning on line 843 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L840-L843

Added lines #L840 - L843 were not covered by tests

self.update_prop(p, orig_handle, legend)
p.set_transform(trans)
p.set_xy([[-xdescent, -ydescent],

Check warning on line 847 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L845-L847

Added lines #L845 - L847 were not covered by tests
[-xdescent + width, -ydescent],
[-xdescent + width, -ydescent + height],
[-xdescent, -ydescent + height]])
return [p]

Check failure on line 851 in lib/matplotlib/legend_handler.py

View workflow job for this annotation

GitHub Actions / ruff

[rdjson] reported by reviewdog 🐶 No newline at end of file Raw Output: message:"No newline at end of file" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/legend_handler.py" range:{start:{line:851 column:19} end:{line:851 column:19}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W292" url:"https://docs.astral.sh/ruff/rules/missing-newline-at-end-of-file"} suggestions:{range:{start:{line:851 column:19} end:{line:851 column:19}} text:"\n"}

Check warning on line 851 in lib/matplotlib/legend_handler.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/legend_handler.py#L851

Added line #L851 was not covered by tests
Loading