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

Commit9800478

Browse files
committed
added test and improve comments for third party backend support of hatchcolors arg
1 parentb9ac0fb commit9800478

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

‎doc/users/next_whats_new/separated_hatchcolor.rst

Lines changed: 3 additions & 3 deletions
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,

‎galleries/examples/shapes_and_collections/hatchcolor_demo.py

Lines changed: 1 addition & 1 deletion
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
)

‎lib/matplotlib/collections.py

Lines changed: 7 additions & 3 deletions
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
exceptTypeError:
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
ifself._gapcolorisnotNone:
@@ -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
forxo,yo,path_id,gc0,rgbFaceinrenderer._iter_collection(

‎lib/matplotlib/tests/test_collections.py

Lines changed: 25 additions & 0 deletions
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+
deftest_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+
defmock_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+
frommatplotlib.collectionsimportPathCollection
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)

[8]ページ先頭

©2009-2025 Movatter.jp