Skip to content

Commit 827e6b1

Browse files
committed
added tests
1 parent fe027ed commit 827e6b1

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed

lib/matplotlib/tests/test_collections.py

+78
Original file line numberDiff line numberDiff line change
@@ -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'])])

lib/matplotlib/tests/test_patches.py

+43
Original file line numberDiff line numberDiff line change
@@ -960,3 +960,46 @@ def test_arrow_set_data():
960960
)
961961
arrow.set_data(x=.5, dx=3, dy=8, width=1.2)
962962
assert np.allclose(expected2, np.round(arrow.get_verts(), 2))
963+
964+
965+
@check_figures_equal(extensions=['png', 'pdf', 'svg', 'eps'], tol=0.021)
966+
def test_patch_hatchcolor(fig_test, fig_ref):
967+
ax_ref = fig_ref.subplots()
968+
ax_test = fig_test.subplots()
969+
970+
# Test that setting hatchcolor works with how hatchcolor was previously controlled
971+
with mpl.rc_context({'hatch.color': 'red'}):
972+
ax_ref.add_patch(Rectangle((0.05, 0.05), 0.4, 0.4, hatch='//'))
973+
ax_test.add_patch(Rectangle((0.05, 0.05), 0.4, 0.4, hatch='//', hatchcolor='red'))
974+
975+
# Test for when edgecolor and hatchcolor is set
976+
# fig_ref uses a workaround to set hatchcolor to blue and edgecolor to red
977+
ax_ref.add_patch(Rectangle((0.05, 0.5), 0.4, 0.4, ec='yellow',
978+
lw=0, hatch='//'))
979+
ax_ref.add_patch(Rectangle((0.05, 0.5), 0.4, 0.4, ec='red', fc='none'))
980+
ax_test.add_patch(Rectangle((0.05, 0.5), 0.4, 0.4, hatch='//', ec='red',
981+
hatchcolor='yellow'))
982+
983+
# Test for explicitly setting edgecolor and then hatchcolor
984+
ax_ref.add_patch(Rectangle((0.5, 0.05), 0.4, 0.4, ec='cyan', lw=0, hatch='//'))
985+
ax_ref.add_patch(Rectangle((0.5, 0.05), 0.4, 0.4, ec='orange', fc='none'))
986+
rect = Rectangle((0.5, 0.05), 0.4, 0.4, hatch='//')
987+
rect.set_edgecolor('orange')
988+
assert rect._hatch_color == mcolors.to_rgba('orange')
989+
rect.set_hatchcolor('cyan')
990+
assert rect._hatch_color == mcolors.to_rgba('cyan')
991+
ax_test.add_patch(rect)
992+
993+
# Test for explicitly setting hatchcolor and then edgecolor
994+
ax_ref.add_patch(Rectangle((0.5, 0.5), 0.4, 0.4, ec='purple', lw=0, hatch='//'))
995+
ax_ref.add_patch(Rectangle((0.5, 0.5), 0.4, 0.4, ec='green', fc='none'))
996+
rect = Rectangle((0.5, 0.5), 0.4, 0.4, hatch='//')
997+
rect.set_hatchcolor('purple')
998+
assert rect._hatch_color == mcolors.to_rgba('purple')
999+
rect.set_edgecolor('green')
1000+
assert rect._hatch_color == mcolors.to_rgba('purple')
1001+
ax_test.add_patch(rect)
1002+
1003+
# Test for default hatch color when edgecolor and hatchcolor is not set
1004+
rect = Rectangle((0, 0), 1, 1, hatch='//')
1005+
assert rect._hatch_color == mcolors.to_rgba(mpl.rcParams['patch.edgecolor'])

0 commit comments

Comments
 (0)