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

Skip to content

Commit 0aea444

Browse files
committed
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 not match 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.
1 parent 9120862 commit 0aea444

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

lib/matplotlib/collections.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,45 @@ def __init__(self, paths, sizes=None, **kwargs):
11311131

11321132
def get_paths(self):
11331133
return self._paths
1134+
1135+
def check_consistency(self):
1136+
"""
1137+
Emit warnings if the lengths of certain properties do not match
1138+
the number of paths.
1139+
1140+
This method checks whether the lengths of `sizes` and `edgecolors`
1141+
are either 1 or equal to the number of paths in the collection.
1142+
If not, a warning is issued.
1143+
1144+
This is useful for identifying potential bugs or silent mismatches
1145+
in collection properties when plotting, especially when properties
1146+
are expected to be applied per-path.
1147+
1148+
Notes
1149+
-----
1150+
- This method does not raise an error, only a warning.
1151+
- It is recommended to call this at draw-time to ensure properties
1152+
have been fully set.
1153+
1154+
Warnings
1155+
--------
1156+
UserWarning
1157+
If the number of sizes or edgecolors does not match the number
1158+
of paths and is not a singleton (length 1).
1159+
1160+
Examples
1161+
--------
1162+
>>> paths = [Path(...), Path(...), Path(...)]
1163+
>>> pc = PathCollection(paths, sizes=[10, 20]) # 3 paths, 2 sizes
1164+
>>> pc.check_consistency()
1165+
UserWarning: Number of sizes does not match number of paths.
1166+
"""
1167+
n_paths = len(self.get_paths())
1168+
if self._sizes is not None and len(self._sizes) not in (1, n_paths):
1169+
warnings.warn("Number of sizes does not match number of paths.")
1170+
if self._edgecolors is not None and len(self._edgecolors) not in (1, n_paths):
1171+
warnings.warn("Number of edgecolors does not match number of paths.")
1172+
11341173

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

12731312
return handles, labels
1313+
1314+
def draw(self, renderer):
1315+
"""
1316+
Draw the collection using the given renderer.
1317+
1318+
This override adds consistency checks before delegating
1319+
to the base implementation.
1320+
1321+
Parameters
1322+
----------
1323+
renderer : RendererBase instance
1324+
The renderer to use for drawing.
1325+
"""
1326+
self.check_consistency()
1327+
return super().draw(renderer)
12741328

12751329

12761330
class PolyCollection(_CollectionWithSizes):

lib/matplotlib/tests/test_collections.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
import matplotlib.path as mpath
1717
import matplotlib.transforms as mtransforms
1818
from matplotlib.collections import (Collection, LineCollection,
19-
EventCollection, PolyCollection)
19+
EventCollection, PolyCollection, PathCollection)
2020
from matplotlib.collections import FillBetweenPolyCollection
2121
from matplotlib.testing.decorators import check_figures_equal, image_comparison
22-
22+
from matplotlib.path import Path
2323

2424
@pytest.fixture(params=["pcolormesh", "pcolor"])
2525
def pcfunc(request):
@@ -1533,3 +1533,12 @@ def mock_draw_path_collection(self, gc, master_transform, paths, all_transforms,
15331533
ax.add_collection(coll)
15341534

15351535
plt.draw()
1536+
1537+
def test_pathcollection_size_mismatch_warning():
1538+
1539+
paths = [Path([(0, 0), (1, 0), (0.5, 1)]) for _ in range(5)]
1540+
fig, ax = plt.subplots()
1541+
with pytest.warns(UserWarning, match="Number of sizes does not match"):
1542+
pc = PathCollection(paths, sizes=[10, 20])
1543+
ax.add_collection(pc)
1544+
fig.canvas.draw()

0 commit comments

Comments
 (0)