Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add consistency check for PathCollection sizes and edgecolors (#28833)#30138

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

Open
BrunoWolf03 wants to merge3 commits intomatplotlib:main
base:main
Choose a base branch
Loading
fromBrunoWolf03:main
Open
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
Add consistency check for PathCollection sizes and edgecolors (#28833)
This patch adds a check_consistency() method to PathCollection,which emits a warning if the number of sizes or edgecolors does notmatch the number of paths. The check is invoked during the draw()call to ensure it happens after all data is set.Also adds a unit test that triggers the warning for mismatched sizes.
  • Loading branch information
@BrunoWolf03
BrunoWolf03 committedJun 4, 2025
commit0aea4447b39e17fefd6bc4457cbdacd59d108e70
54 changes: 54 additions & 0 deletionslib/matplotlib/collections.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1131,6 +1131,45 @@

def get_paths(self):
return self._paths

Check failure on line 1134 in lib/matplotlib/collections.py

View workflow job for this annotation

GitHub Actions/ ruff

[rdjson] reported by reviewdog 🐶Blank line contains whitespaceRaw Output:message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/collections.py" range:{start:{line:1134 column:1} end:{line:1134 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:1134 column:1} end:{line:1134 column:5}}}
def check_consistency(self):
"""
Emit warnings if the lengths of certain properties do not match
the number of paths.

This method checks whether the lengths of `sizes` and `edgecolors`
are either 1 or equal to the number of paths in the collection.
If not, a warning is issued.

This is useful for identifying potential bugs or silent mismatches
in collection properties when plotting, especially when properties
are expected to be applied per-path.

Notes
-----
- This method does not raise an error, only a warning.
- It is recommended to call this at draw-time to ensure properties
have been fully set.

Warnings
--------
UserWarning
If the number of sizes or edgecolors does not match the number
of paths and is not a singleton (length 1).

Examples
--------
>>> paths = [Path(...), Path(...), Path(...)]
>>> pc = PathCollection(paths, sizes=[10, 20]) # 3 paths, 2 sizes
>>> pc.check_consistency()
UserWarning: Number of sizes does not match number of paths.
"""
n_paths = len(self.get_paths())
if self._sizes is not None and len(self._sizes) not in (1, n_paths):
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
ifself._sizesisnotNoneandlen(self._sizes)notin (1,n_paths):
ifself._sizes.sizenotin (0,1,n_paths):

Looking at theset_sizes method, we always end up with an array here, though it may be size zero (if None was passed). I think this is the cause of at least some of the failing tests (any warnings are translated into errors in the tests).

warnings.warn("Number of sizes does not match number of paths.")
if self._edgecolors is not None and len(self._edgecolors) not in (1, n_paths):
warnings.warn("Number of edgecolors does not match number of paths.")


def legend_elements(self, prop="colors", num="auto",
fmt=None, func=lambda x: x, **kwargs):
Expand DownExpand Up@@ -1271,6 +1310,21 @@
labels.append(l)

return handles, labels

Check failure on line 1313 in lib/matplotlib/collections.py

View workflow job for this annotation

GitHub Actions/ ruff

[rdjson] reported by reviewdog 🐶Blank line contains whitespaceRaw Output:message:"Blank line contains whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/collections.py" range:{start:{line:1313 column:1} end:{line:1313 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:1313 column:1} end:{line:1313 column:5}}}
def draw(self, renderer):
"""
Draw the collection using the given renderer.

This override adds consistency checks before delegating
to the base implementation.

Parameters
----------
renderer : RendererBase instance
The renderer to use for drawing.
"""
self.check_consistency()
return super().draw(renderer)


class PolyCollection(_CollectionWithSizes):
Expand Down
13 changes: 11 additions & 2 deletionslib/matplotlib/tests/test_collections.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -16,12 +16,12 @@
import matplotlib.path as mpath
import matplotlib.transforms as mtransforms
from matplotlib.collections import (Collection, LineCollection,
EventCollection, PolyCollection)
EventCollection, PolyCollection, PathCollection)
from matplotlib.collections import FillBetweenPolyCollection
from matplotlib.testing.decorators import check_figures_equal, image_comparison

from matplotlib.path import Path

@pytest.fixture(params=["pcolormesh", "pcolor"])

Check failure on line 24 in lib/matplotlib/tests/test_collections.py

View workflow job for this annotation

GitHub Actions/ ruff

[rdjson] reported by reviewdog 🐶Expected 2 blank lines, found 1Raw Output:message:"Expected 2 blank lines, found 1" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_collections.py" range:{start:{line:24 column:1} end:{line:24 column:2}}} 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:23 column:1} end:{line:24 column:1}} text:"\n\n"}
def pcfunc(request):
return request.param

Expand DownExpand Up@@ -1533,3 +1533,12 @@
ax.add_collection(coll)

plt.draw()

def test_pathcollection_size_mismatch_warning():

Check failure on line 1537 in lib/matplotlib/tests/test_collections.py

View workflow job for this annotation

GitHub Actions/ ruff

[rdjson] reported by reviewdog 🐶Expected 2 blank lines, found 1Raw Output:message:"Expected 2 blank lines, found 1" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_collections.py" range:{start:{line:1537 column:1} end:{line:1537 column:4}}} 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:1536 column:1} end:{line:1537 column:1}} text:"\n\n"}

paths = [Path([(0, 0), (1, 0), (0.5, 1)]) for _ in range(5)]
fig, ax = plt.subplots()
with pytest.warns(UserWarning, match="Number of sizes does not match"):
pc = PathCollection(paths, sizes=[10, 20])
ax.add_collection(pc)
fig.canvas.draw()

Check failure on line 1544 in lib/matplotlib/tests/test_collections.py

View workflow job for this annotation

GitHub Actions/ ruff

[rdjson] reported by reviewdog 🐶Trailing whitespaceRaw Output:message:"Trailing whitespace" location:{path:"/home/runner/work/matplotlib/matplotlib/lib/matplotlib/tests/test_collections.py" range:{start:{line:1544 column:26} end:{line:1544 column:28}}} source:{name:"ruff" url:"https://docs.astral.sh/ruff"} code:{value:"W291" url:"https://docs.astral.sh/ruff/rules/trailing-whitespace"} suggestions:{range:{start:{line:1544 column:26} end:{line:1544 column:28}}}
Loading

[8]ページ先頭

©2009-2025 Movatter.jp