@@ -1331,3 +1331,81 @@ def test_striped_lines(fig_test, fig_ref, gapcolor):
13311331 for x , gcol , ls in zip (x , itertools .cycle (gapcolor ),
13321332 itertools .cycle (linestyles )):
13331333 ax_ref .axvline (x , 0 , 1 , linestyle = ls , gapcolor = gcol , alpha = 0.5 )
1334+
1335+
1336+ @check_figures_equal (extensions = ['png' , 'pdf' , 'svg' , 'eps' ], tol = 0.026 )
1337+ def test_collection_hatchcolor (fig_test , fig_ref ):
1338+ from matplotlib .collections import PathCollection
1339+
1340+ ax_test = fig_test .subplots ()
1341+ ax_ref = fig_ref .subplots ()
1342+
1343+ rect = mpath .Path .unit_rectangle ().deepcopy ()
1344+ rect .vertices *= [0.2 , 0.2 ]
1345+
1346+ # Test that setting hatchcolor works with how hatchcolor was previously controlled
1347+ r = rect .deepcopy ()
1348+ r .vertices = rect .vertices + [0.05 , 0.05 ]
1349+ with mpl .rc_context ({"hatch.color" : "red" }):
1350+ ax_ref .add_collection (PathCollection ([r ], hatch = '/' , edgecolor = 'blue' ))
1351+ ax_test .add_collection (PathCollection ([r ], hatch = '/' , edgecolor = 'blue' ,
1352+ hatchcolor = 'red' ))
1353+
1354+ color_list_1 = ['purple' , 'red' , 'green' , 'yellow' ]
1355+ color_list_2 = ['yellow' , 'green' , 'red' , 'purple' ]
1356+
1357+ # Test for when edgecolor and hatchcolor is set
1358+ # fig_ref uses a workaround to set the hatchcolor different from the edgecolor
1359+ r = [mpl .path .Path (rect .vertices + [0.3 , 0.05 + 0.25 * i ]) for i in range (4 )]
1360+ ax_ref .add_collection (PathCollection (r , hatch = '//' , lw = 0 ,
1361+ edgecolor = color_list_1 ))
1362+ ax_ref .add_collection (PathCollection (r , fc = 'none' ,
1363+ edgecolor = color_list_2 ))
1364+ ax_test .add_collection (PathCollection (r , hatch = '//' ,
1365+ edgecolor = color_list_2 ,
1366+ hatchcolor = color_list_1 ))
1367+
1368+ # Test for explicitly setting edgecolor and then hatchcolor
1369+ r = [mpl .path .Path (e .vertices + [0.25 , 0 ]) for e in r ]
1370+ ax_ref .add_collection (PathCollection (r , hatch = '//' , lw = 0 ,
1371+ edgecolor = color_list_1 ))
1372+ ax_ref .add_collection (PathCollection (r , fc = 'none' ,
1373+ edgecolor = color_list_2 ))
1374+ col = PathCollection (r , hatch = '//' )
1375+ col .set_edgecolor (color_list_2 )
1376+ assert_array_equal (col .get_hatchcolor (), [mpl .colors .to_rgba (c )
1377+ for c in color_list_2 ])
1378+ col .set_hatchcolor (color_list_1 )
1379+ assert_array_equal (col .get_hatchcolor (), [mpl .colors .to_rgba (c )
1380+ for c in color_list_1 ])
1381+ ax_test .add_collection (col )
1382+
1383+ # Test for explicitly setting hatchcolor and then edgecolor
1384+ r = [mpl .path .Path (e .vertices + [0.25 , 0 ]) for e in r ]
1385+ ax_ref .add_collection (PathCollection (r , hatch = '//' , lw = 0 ,
1386+ edgecolor = color_list_1 ))
1387+ ax_ref .add_collection (PathCollection (r , fc = 'none' ,
1388+ edgecolor = color_list_2 ))
1389+ col = PathCollection (r , hatch = '//' )
1390+ col .set_hatchcolor (color_list_1 )
1391+ assert_array_equal (col .get_hatchcolor (), [mpl .colors .to_rgba (c )
1392+ for c in color_list_1 ])
1393+ col .set_edgecolor (color_list_2 )
1394+ assert_array_equal (col .get_hatchcolor (), [mpl .colors .to_rgba (c )
1395+ for c in color_list_1 ])
1396+ ax_test .add_collection (col )
1397+
1398+ # Test for default hatch color when edgecolor and hatchcolor is not set
1399+ col = PathCollection ([rect ], hatch = '//' )
1400+ assert_array_equal (col .get_hatchcolor (), [mpl .colors .to_rgba (
1401+ mpl .rcParams ['patch.edgecolor' ])])
1402+
1403+ # Test for hatch color when edgecolor is set to 'none' and hatchcolor is not set
1404+ col = PathCollection ([rect ], hatch = '//' , edgecolor = 'none' )
1405+ assert_array_equal (col .get_hatchcolor (), [mpl .colors .to_rgba (
1406+ mpl .rcParams ['patch.edgecolor' ])])
1407+
1408+ # Test for hatch color when edgecolor is set to 'face' and hatchcolor is not set
1409+ col = PathCollection ([rect ], hatch = '//' , edgecolor = 'face' )
1410+ assert_array_equal (col .get_hatchcolor (), [mpl .colors .to_rgba (
1411+ mpl .rcParams ['patch.edgecolor' ])])
0 commit comments