Skip to content

Commit b59c814

Browse files
authored
Merge pull request matplotlib#20692 from anntzer/hatch
Small cleanups to hatch.py.
2 parents 0cebec2 + 79e076d commit b59c814

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``hatch.SmallFilledCircles`` now inherits from ``hatch.Circles``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
... rather than from ``hatch.SmallCircles``.

lib/matplotlib/hatch.py

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -101,28 +101,24 @@ def __init__(self, hatch, density):
101101
def set_vertices_and_codes(self, vertices, codes):
102102
offset = 1.0 / self.num_rows
103103
shape_vertices = self.shape_vertices * offset * self.size
104-
if not self.filled:
105-
inner_vertices = shape_vertices[::-1] * 0.9
106104
shape_codes = self.shape_codes
107-
shape_size = len(shape_vertices)
108-
109-
cursor = 0
105+
if not self.filled:
106+
shape_vertices = np.concatenate( # Forward, then backward.
107+
[shape_vertices, shape_vertices[::-1] * 0.9])
108+
shape_codes = np.concatenate([shape_codes, shape_codes])
109+
vertices_parts = []
110+
codes_parts = []
110111
for row in range(self.num_rows + 1):
111112
if row % 2 == 0:
112113
cols = np.linspace(0, 1, self.num_rows + 1)
113114
else:
114115
cols = np.linspace(offset / 2, 1 - offset / 2, self.num_rows)
115116
row_pos = row * offset
116117
for col_pos in cols:
117-
vertices[cursor:cursor + shape_size] = (shape_vertices +
118-
(col_pos, row_pos))
119-
codes[cursor:cursor + shape_size] = shape_codes
120-
cursor += shape_size
121-
if not self.filled:
122-
vertices[cursor:cursor + shape_size] = (inner_vertices +
123-
(col_pos, row_pos))
124-
codes[cursor:cursor + shape_size] = shape_codes
125-
cursor += shape_size
118+
vertices_parts.append(shape_vertices + [col_pos, row_pos])
119+
codes_parts.append(shape_codes)
120+
np.concatenate(vertices_parts, out=vertices)
121+
np.concatenate(codes_parts, out=codes)
126122

127123

128124
class Circles(Shapes):
@@ -149,16 +145,13 @@ def __init__(self, hatch, density):
149145
super().__init__(hatch, density)
150146

151147

152-
# TODO: __init__ and class attributes override all attributes set by
153-
# SmallCircles. Should this class derive from Circles instead?
154-
class SmallFilledCircles(SmallCircles):
148+
class SmallFilledCircles(Circles):
155149
size = 0.1
156150
filled = True
157151

158152
def __init__(self, hatch, density):
159153
self.num_rows = (hatch.count('.')) * density
160-
# Not super().__init__!
161-
Circles.__init__(self, hatch, density)
154+
super().__init__(hatch, density)
162155

163156

164157
class Stars(Shapes):

0 commit comments

Comments
 (0)