Skip to content

Commit 0c557e4

Browse files
committed
added test and improved comments
1 parent b9ac0fb commit 0c557e4

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

Diff for: doc/users/next_whats_new/separated_hatchcolor.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ to 'patch.edgecolor' with the alpha value of the collection.
7878

7979
fig, ax = plt.subplots()
8080

81-
x = np.random.rand(20)
82-
y = np.random.rand(20)
83-
colors = ["blue", "orange", "green"]
81+
x = [29, 36, 41, 25, 32, 70, 62, 58, 66, 80, 58, 68, 62, 37, 48]
82+
y = [82, 76, 48, 53, 62, 70, 84, 68, 55, 75, 29, 25, 12, 17, 20]
83+
colors = ['tab:blue'] * 5 + ['tab:orange'] * 5 + ['tab:green'] * 5
8484

8585
ax.scatter(
8686
x,

Diff for: galleries/examples/shapes_and_collections/hatchcolor_demo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
facecolor="none",
7070
edgecolor="gray",
7171
linewidth=2,
72-
marker="s", # Use hexagon as marker
72+
marker="h", # Use hexagon as marker
7373
hatch="xxx",
7474
hatchcolor=colors,
7575
)

Diff for: lib/matplotlib/collections.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,10 @@ def draw(self, renderer):
423423
# The current new API of draw_path_collection() is provisional
424424
# and will be changed in a future PR.
425425

426-
# Find whether renderer.draw_path_collection() takes hatchcolor parameter
426+
# Find whether renderer.draw_path_collection() takes hatchcolor parameter.
427+
# Since third-party implementations of draw_path_collection() may not be
428+
# introspectable, e.g. with inspect.signature, the only way is to try and
429+
# call this with the hatchcolors parameter.
427430
hatchcolors_arg_supported = True
428431
try:
429432
renderer.draw_path_collection(
@@ -435,6 +438,9 @@ def draw(self, renderer):
435438
"screen", hatchcolors=self.get_hatchcolor()
436439
)
437440
except TypeError:
441+
# If the renderer does not support the hatchcolors argument,
442+
# it will raise a TypeError. In this case, we will
443+
# iterate over all paths and draw them one by one.
438444
hatchcolors_arg_supported = False
439445

440446
if self._gapcolor is not None:
@@ -449,8 +455,6 @@ def draw(self, renderer):
449455
self.get_transforms(), *args,
450456
hatchcolors=self.get_hatchcolor())
451457
else:
452-
# If the renderer does not support the hatchcolors argument,
453-
# iterate over the paths and draw them one by one.
454458
path_ids = renderer._iter_collection_raw_paths(
455459
transform.frozen(), ipaths, self.get_transforms())
456460
for xo, yo, path_id, gc0, rgbFace in renderer._iter_collection(

Diff for: lib/matplotlib/tests/test_collections.py

+25
Original file line numberDiff line numberDiff line change
@@ -1486,3 +1486,28 @@ def test_draw_path_collection_no_hatchcolor(backend):
14861486
facecolors, edgecolors, linewidths, linestyles,
14871487
antialiaseds, urls, offset_position
14881488
)
1489+
1490+
1491+
def test_third_party_backend_hatchcolors_arg_fallback(monkeypatch):
1492+
fig, ax = plt.subplots()
1493+
canvas = fig.canvas
1494+
renderer = canvas.get_renderer()
1495+
1496+
# monkeypatch the `draw_path_collection` method to simulate a third-party backend
1497+
# that does not support the `hatchcolors` argument.
1498+
def mock_draw_path_collection(self, gc, master_transform, paths, all_transforms,
1499+
offsets, offset_trans, facecolors, edgecolors,
1500+
linewidths, linestyles, antialiaseds, urls,
1501+
offset_position):
1502+
pass
1503+
1504+
monkeypatch.setattr(renderer, 'draw_path_collection', mock_draw_path_collection)
1505+
1506+
# Create a PathCollection with hatch colors
1507+
from matplotlib.collections import PathCollection
1508+
path = mpath.Path.unit_rectangle()
1509+
coll = PathCollection([path], hatch='//', hatchcolor='red')
1510+
1511+
ax.add_collection(coll)
1512+
1513+
plt.draw()

0 commit comments

Comments
 (0)