@@ -225,22 +225,24 @@ def get_path(hatchpattern, density=6):
225
225
return Path (vertices , codes )
226
226
227
227
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
+ }
238
238
239
239
240
240
class HatchStyle :
241
241
def __init__ (self , hatchpattern , ** kwargs ):
242
242
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
+ }
244
246
245
247
def rotate_vertices (self , vertices , angle = None , scale_correction = True ):
246
248
vertices = vertices .copy ()
@@ -308,7 +310,7 @@ class MarkerHatchStyle(HatchStyle):
308
310
def _get_marker_path (marker ):
309
311
func = np .atleast_1d (MarkerHatchStyle .marker_paths [marker ])
310
312
path = func [0 ](* func [1 :])
311
- size = MarkerHatchStyle .marker_sizes [ marker ]
313
+ size = MarkerHatchStyle .marker_sizes . get ( marker , attrs [ 'weight' ])
312
314
313
315
return Path (
314
316
vertices = path .vertices * size ,
@@ -321,34 +323,46 @@ def marker_pattern(hatchstyle):
321
323
hatchstyle .kwargs ['scale' ] * hatchstyle .hatch_buffer_size / 100.0
322
324
)
323
325
path = MarkerHatchStyle ._get_marker_path (hatchstyle .hatchpattern )
324
- shape_vertices = hatchstyle .rotate_vertices (
326
+ marker_vertices = hatchstyle .rotate_vertices (
325
327
path .vertices , scale_correction = False
326
328
)
327
- shape_codes = path .codes
329
+ marker_codes = path .codes
328
330
329
331
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
331
335
332
336
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 ]
335
339
)
336
- shape_codes = np .concatenate ([shape_codes , shape_codes ])
340
+ marker_codes = np .concatenate ([marker_codes , marker_codes ])
337
341
338
- vertices_parts = []
339
- codes_parts = []
342
+ vertices = np . empty (( 0 , 2 ))
343
+ codes = np . empty ( 0 , Path . code_type )
340
344
for row in range (num_rows + 1 ):
345
+ row_pos = row * offset
341
346
if row % 2 == 0 :
342
347
cols = np .linspace (0 , 1 , num_rows + 1 )
343
348
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 ]
349
355
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 ))
352
366
353
367
return vertices , codes
354
368
0 commit comments