Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c9ee65

Browse files
committed
added random rotation, placement and staggering
1 parent c4870d6 commit 6c9ee65

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

lib/matplotlib/hatch.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -225,22 +225,24 @@ def get_path(hatchpattern, density=6):
225225
return Path(vertices, codes)
226226

227227

228-
attrs = [
229-
# ("color", "black"), For now it is an attr of gc
230-
# ("alpha", 1.0),
231-
('scale', 6),
232-
('weight', 1.0),
233-
('angle', 0.0),
234-
('random_rotation', False),
235-
('staggered', False),
236-
('filled', True),
237-
]
228+
attrs = {
229+
'scale': 6,
230+
'weight': 1.0,
231+
'angle': 0.0,
232+
'random_rotation': False,
233+
'random_placement': False,
234+
'x_stagger': 0.5,
235+
'y_stagger': 0.0,
236+
'filled': True,
237+
}
238238

239239

240240
class HatchStyle:
241241
def __init__(self, hatchpattern, **kwargs):
242242
self.hatchpattern = hatchpattern
243-
self.kwargs = {attr: kwargs.get(attr, default) for attr, default in attrs}
243+
self.kwargs = {
244+
attr: kwargs.get(attr, default) for attr, default in attrs.items()
245+
}
244246

245247
def rotate_vertices(self, vertices, angle=None, scale_correction=True):
246248
vertices = vertices.copy()
@@ -308,7 +310,7 @@ class MarkerHatchStyle(HatchStyle):
308310
def _get_marker_path(marker):
309311
func = np.atleast_1d(MarkerHatchStyle.marker_paths[marker])
310312
path = func[0](*func[1:])
311-
size = MarkerHatchStyle.marker_sizes[marker]
313+
size = MarkerHatchStyle.marker_sizes.get(marker, attrs['weight'])
312314

313315
return Path(
314316
vertices=path.vertices * size,
@@ -321,34 +323,46 @@ def marker_pattern(hatchstyle):
321323
hatchstyle.kwargs['scale'] * hatchstyle.hatch_buffer_size / 100.0
322324
)
323325
path = MarkerHatchStyle._get_marker_path(hatchstyle.hatchpattern)
324-
shape_vertices = hatchstyle.rotate_vertices(
326+
marker_vertices = hatchstyle.rotate_vertices(
325327
path.vertices, scale_correction=False
326328
)
327-
shape_codes = path.codes
329+
marker_codes = path.codes
328330

329331
offset = 1.0 / num_rows
330-
shape_vertices = shape_vertices * offset * size
332+
marker_vertices = marker_vertices * offset * size
333+
x_stagger = hatchstyle.kwargs['x_stagger'] * offset
334+
y_stagger = hatchstyle.kwargs['y_stagger'] * offset
331335

332336
if not hatchstyle.kwargs['filled']:
333-
shape_vertices = np.concatenate(
334-
[shape_vertices, shape_vertices[::-1] * 0.9]
337+
marker_vertices = np.concatenate(
338+
[marker_vertices, marker_vertices[::-1] * 0.9]
335339
)
336-
shape_codes = np.concatenate([shape_codes, shape_codes])
340+
marker_codes = np.concatenate([marker_codes, marker_codes])
337341

338-
vertices_parts = []
339-
codes_parts = []
342+
vertices = np.empty((0, 2))
343+
codes = np.empty(0, Path.code_type)
340344
for row in range(num_rows + 1):
345+
row_pos = row * offset
341346
if row % 2 == 0:
342347
cols = np.linspace(0, 1, num_rows + 1)
343348
else:
344-
cols = np.linspace(offset / 2, 1 - offset / 2, num_rows)
345-
row_pos = row * offset
346-
for col_pos in cols:
347-
vertices_parts.append(shape_vertices + [col_pos, row_pos])
348-
codes_parts.append(shape_codes)
349+
cols = np.linspace(x_stagger, 1 + x_stagger, num_rows + 1)
350+
351+
for i, col_pos in enumerate(cols):
352+
vertices_part = marker_vertices + [col_pos, row_pos]
353+
if i % 2 == 1:
354+
vertices_part += [0, y_stagger]
349355

350-
vertices = np.concatenate(vertices_parts)
351-
codes = np.concatenate(codes_parts)
356+
if hatchstyle.kwargs['random_rotation']:
357+
vertices_part = hatchstyle.rotate_vertices(
358+
vertices_part, np.random.uniform(0, 360), scale_correction=False
359+
)
360+
361+
if hatchstyle.kwargs['random_placement']:
362+
vertices_part += np.random.uniform(-offset / 4, offset / 4, 2)
363+
364+
vertices = np.concatenate((vertices, vertices_part))
365+
codes = np.concatenate((codes, marker_codes))
352366

353367
return vertices, codes
354368

0 commit comments

Comments
 (0)