Skip to content

Commit 16a7cf2

Browse files
committed
Added hatch test
1 parent 7626ba5 commit 16a7cf2

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

test/test_hatch.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from helpers import assert_equality
2+
3+
4+
def plot():
5+
"""
6+
Hatch demo code from
7+
https://matplotlib.org/3.1.1/gallery/shapes_and_collections/hatch_demo.html
8+
9+
Slightly modified to test more aspects of the hatch implementation
10+
"""
11+
import matplotlib.pyplot as plt
12+
from matplotlib.patches import Ellipse, Polygon
13+
import numpy as np
14+
15+
fig, (ax1, ax2, ax3) = plt.subplots(3)
16+
17+
ax1.bar(range(1, 5), range(1, 5), color="red", edgecolor="black", hatch="/")
18+
ax1.bar(
19+
range(1, 5),
20+
[6] * 4,
21+
bottom=range(1, 5),
22+
color="blue",
23+
edgecolor="black",
24+
hatch="//",
25+
)
26+
ax1.set_xticks([1.5, 2.5, 3.5, 4.5])
27+
28+
bars = ax2.bar(
29+
range(1, 5), range(1, 5), color="yellow", edgecolor="black"
30+
) + ax2.bar(range(1, 5), [6] * 4, bottom=range(1, 5), color="green")
31+
ax2.set_xticks([1.5, 2.5, 3.5, 4.5])
32+
33+
patterns = ("-", "+", "x", "\\", "*", "o", "O", ".")
34+
for bar, pattern in zip(bars, patterns):
35+
bar.set_hatch(pattern)
36+
37+
ax3.fill(
38+
[1, 3, 3, 1], [1, 1, 2, 2], fill=False, hatch="\\", zorder=1, label="Square"
39+
)
40+
ax3.add_patch(
41+
Ellipse((4, 1.5), 4, 0.5, fill="green", hatch="*", zorder=3, label="Ellipse")
42+
)
43+
p = Polygon(
44+
[[0, 0], [4, 1.1], [6, 2.5], [2, 1.4]],
45+
closed=True,
46+
fill=False,
47+
hatch="/",
48+
zorder=2,
49+
label="Polygon",
50+
)
51+
p._hatch_color = (0.5, 0.3, 0.8, 0.7)
52+
ax3.add_patch(p)
53+
ax3.set_xlim((0, 6))
54+
ax3.set_ylim((0, 2.5))
55+
ax3.legend()
56+
57+
return fig
58+
59+
60+
def test():
61+
assert_equality(plot, "test_hatch_reference.tex")
62+
63+
64+
if __name__ == "__main__":
65+
import helpers
66+
67+
helpers.compare_mpl_latex(plot)

test/test_hatch_reference.tex

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
\begin{tikzpicture}
2+
3+
\definecolor{color0}{rgb}{1,1,0}
4+
\definecolor{color1}{rgb}{0.12156862745098,0.466666666666667,0.705882352941177}
5+
\definecolor{color2}{rgb}{0.5,0.3,0.8}
6+
7+
\begin{groupplot}[group style={group size=1 by 3}]
8+
\nextgroupplot[
9+
tick align=outside,
10+
tick pos=left,
11+
x grid style={white!69.01960784313725!black},
12+
xmin=0.41, xmax=4.59,
13+
xtick style={color=black},
14+
y grid style={white!69.01960784313725!black},
15+
ymin=0, ymax=10.5,
16+
ytick style={color=black}
17+
]
18+
\draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:0.6,0) rectangle (axis cs:1.4,1);
19+
\draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:1.6,0) rectangle (axis cs:2.4,2);
20+
\draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:2.6,0) rectangle (axis cs:3.4,3);
21+
\draw[draw=black,fill=red,postaction={pattern=north east lines}] (axis cs:3.6,0) rectangle (axis cs:4.4,4);
22+
\draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:0.6,1) rectangle (axis cs:1.4,7);
23+
\draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:1.6,2) rectangle (axis cs:2.4,8);
24+
\draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:2.6,3) rectangle (axis cs:3.4,9);
25+
\draw[draw=black,fill=blue,postaction={pattern=north east lines}] (axis cs:3.6,4) rectangle (axis cs:4.4,10);
26+
27+
\nextgroupplot[
28+
tick align=outside,
29+
tick pos=left,
30+
x grid style={white!69.01960784313725!black},
31+
xmin=0.41, xmax=4.59,
32+
xtick style={color=black},
33+
y grid style={white!69.01960784313725!black},
34+
ymin=0, ymax=10.5,
35+
ytick style={color=black}
36+
]
37+
\draw[draw=black,fill=color0,postaction={pattern=horizontal lines}] (axis cs:0.6,0) rectangle (axis cs:1.4,1);
38+
\draw[draw=black,fill=color0,postaction={pattern=grid}] (axis cs:1.6,0) rectangle (axis cs:2.4,2);
39+
\draw[draw=black,fill=color0,postaction={pattern=crosshatch}] (axis cs:2.6,0) rectangle (axis cs:3.4,3);
40+
\draw[draw=black,fill=color0,postaction={pattern=north west lines}] (axis cs:3.6,0) rectangle (axis cs:4.4,4);
41+
\draw[draw=none,fill=green!50.19607843137255!black,postaction={pattern=fivepointed stars}] (axis cs:0.6,1) rectangle (axis cs:1.4,7);
42+
\draw[draw=none,fill=green!50.19607843137255!black,postaction={pattern=sixpointed stars}] (axis cs:1.6,2) rectangle (axis cs:2.4,8);
43+
\draw[draw=none,fill=green!50.19607843137255!black,postaction={pattern=bricks}] (axis cs:2.6,3) rectangle (axis cs:3.4,9);
44+
\draw[draw=none,fill=green!50.19607843137255!black,postaction={pattern=crosshatch dots}] (axis cs:3.6,4) rectangle (axis cs:4.4,10);
45+
46+
\nextgroupplot[
47+
legend cell align={left},
48+
legend style={fill opacity=0.8, draw opacity=1, text opacity=1, draw=white!80.0!black},
49+
tick align=outside,
50+
tick pos=left,
51+
x grid style={white!69.01960784313725!black},
52+
xmin=0, xmax=6,
53+
xtick style={color=black},
54+
y grid style={white!69.01960784313725!black},
55+
ymin=0, ymax=2.5,
56+
ytick style={color=black}
57+
]
58+
\path [draw=black, postaction={pattern=north west lines}]
59+
(axis cs:1,1)
60+
--(axis cs:3,1)
61+
--(axis cs:3,2)
62+
--(axis cs:1,2)
63+
--cycle;
64+
\addlegendimage{area legend, draw=black, postaction={pattern=north west lines}}
65+
\addlegendentry{Square}
66+
67+
\path [draw=black, postaction={pattern=north east lines, pattern color=color2, fill opacity=0.7}]
68+
(axis cs:0,0)
69+
--(axis cs:4,1.1)
70+
--(axis cs:6,2.5)
71+
--(axis cs:2,1.4)
72+
--cycle;
73+
\addlegendimage{area legend, draw=black, postaction={pattern=north east lines, pattern color=color2, fill opacity=0.7}}
74+
\addlegendentry{Polygon}
75+
76+
\draw[draw=none,fill=color1,postaction={pattern=fivepointed stars}] (axis cs:4,1.5) ellipse (2 and 0.25);
77+
\addlegendimage{area legend, draw=none, fill=color1, postaction={pattern=fivepointed stars}}
78+
\addlegendentry{Ellipse}
79+
80+
\end{groupplot}
81+
82+
\end{tikzpicture}

0 commit comments

Comments
 (0)