Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
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
base:main
Are you sure you want to change the base?
Changes from1 commit
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
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
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1131,6 +1131,45 @@ | ||||||
def get_paths(self): | ||||||
return self._paths | ||||||
Check failure on line 1134 in lib/matplotlib/collections.py
| ||||||
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): | ||||||
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. Suggested change
Looking at the | ||||||
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): | ||||||
@@ -1271,6 +1310,21 @@ | ||||||
labels.append(l) | ||||||
return handles, labels | ||||||
Check failure on line 1313 in lib/matplotlib/collections.py
| ||||||
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): | ||||||
Uh oh!
There was an error while loading.Please reload this page.