@@ -104,12 +104,14 @@ def _arc(self, quadrant=0, cw=True, radius=1, center=(0,0)):
104
104
# Negate x.
105
105
if cw :
106
106
# Swap x and y.
107
- vertices = np .column_stack ((- ARC_VERTICES [:,1 ], ARC_VERTICES [:,0 ]))
107
+ vertices = np .column_stack ((- ARC_VERTICES [:,1 ],
108
+ ARC_VERTICES [:,0 ]))
108
109
else :
109
- vertices = np .column_stack ((- ARC_VERTICES [:,0 ], ARC_VERTICES [:,1 ]))
110
+ vertices = np .column_stack ((- ARC_VERTICES [:,0 ],
111
+ ARC_VERTICES [:,1 ]))
110
112
if quadrant > 1 : radius = - radius # Rotate 180 deg.
111
- return zip (ARC_CODES ,
112
- radius * vertices + np .tile (center , (ARC_VERTICES .shape [0 ], 1 )))
113
+ return zip (ARC_CODES , radius * vertices +
114
+ np .tile (center , (ARC_VERTICES .shape [0 ], 1 )))
113
115
114
116
def _add_input (self , path , angle , flow , length ):
115
117
"""Add an input to a path and return its tip and label locations.
@@ -196,9 +198,11 @@ def _add_output(self, path, angle, flow, length):
196
198
else :
197
199
path .append ((Path .LINETO , [x , y ]))
198
200
path .extend ([(Path .LINETO , [x , y + sign * length ]),
199
- (Path .LINETO , [x - self .shoulder , y + sign * length ]),
201
+ (Path .LINETO , [x - self .shoulder ,
202
+ y + sign * length ]),
200
203
(Path .LINETO , tip ),
201
- (Path .LINETO , [x + self .shoulder - flow , y + sign * length ]),
204
+ (Path .LINETO , [x + self .shoulder - flow ,
205
+ y + sign * length ]),
202
206
(Path .LINETO , [x - flow , y + sign * length ])])
203
207
path .extend (self ._arc (quadrant = quadrant ,
204
208
cw = angle == DOWN ,
@@ -317,13 +321,13 @@ def add(self, patchlabel='', flows=np.array([1.0,-1.0]), orientations=[0,0],
317
321
if rotation == None :
318
322
rotation = 0
319
323
else :
320
- rotation /= 90.0 # In the code below, angles are expressed in deg/90.
324
+ rotation /= 90.0 # In the code below, angles are expressed in deg/90
321
325
assert len (orientations ) == n , ("orientations and flows must have the "
322
326
"same length.\n orientations has length "
323
327
"%d, but flows has length %d."
324
328
% len (orientations ), n )
325
329
if getattr (labels , '__iter__' , False ):
326
- # iterable() isn't used because it would give True if labels is a string.
330
+ # iterable() isn't used because it would give True if labels is a string
327
331
assert len (labels ) == n , ("If labels is a list, then labels and "
328
332
"flows must have the same length.\n "
329
333
"labels has length %d, but flows has "
@@ -422,7 +426,8 @@ def add(self, patchlabel='', flows=np.array([1.0,-1.0]), orientations=[0,0],
422
426
lllength = pathlengths
423
427
d = dict (RIGHT = pathlengths )
424
428
pathlengths = [d .get (angle , 0 ) for angle in angles ]
425
- # Determine the lengths of the top-side arrows from the middle outwards.
429
+ # Determine the lengths of the top-side arrows
430
+ # from the middle outwards.
426
431
for i , (angle , is_input , flow ) \
427
432
in enumerate (zip (angles , are_inputs , scaled_flows )):
428
433
if angle == DOWN and is_input :
@@ -431,7 +436,8 @@ def add(self, patchlabel='', flows=np.array([1.0,-1.0]), orientations=[0,0],
431
436
elif angle == UP and not is_input :
432
437
pathlengths [i ] = urlength
433
438
urlength -= flow # Flow is negative for outputs.
434
- # Determine the lengths of the bottom-side arrows from the middle outwards.
439
+ # Determine the lengths of the bottom-side arrows
440
+ # from the middle outwards.
435
441
for i , (angle , is_input , flow ) \
436
442
in enumerate (zip (angles , are_inputs , scaled_flows )[::- 1 ]):
437
443
if angle == UP and is_input :
@@ -440,7 +446,8 @@ def add(self, patchlabel='', flows=np.array([1.0,-1.0]), orientations=[0,0],
440
446
elif angle == DOWN and not is_input :
441
447
pathlengths [n - i - 1 ] = lrlength
442
448
lrlength -= flow
443
- # Determine the lengths of the left-side arrows from the bottom upwards.
449
+ # Determine the lengths of the left-side arrows
450
+ # from the bottom upwards.
444
451
has_left_input = False
445
452
for i , (angle , is_input , spec ) \
446
453
in enumerate (zip (angles , are_inputs , zip (scaled_flows ,
@@ -451,7 +458,8 @@ def add(self, patchlabel='', flows=np.array([1.0,-1.0]), orientations=[0,0],
451
458
pathlengths [n - i - 1 ] = 0
452
459
else :
453
460
has_left_input = True
454
- # Determine the lengths of the right-side arrows from the top downwards.
461
+ # Determine the lengths of the right-side arrows
462
+ # from the top downwards.
455
463
has_right_output = False
456
464
for i , (angle , is_input , spec ) \
457
465
in enumerate (zip (angles , are_inputs , zip (scaled_flows ,
@@ -499,40 +507,57 @@ def add(self, patchlabel='', flows=np.array([1.0,-1.0]), orientations=[0,0],
499
507
label_locations = np .zeros ((n ,2 ))
500
508
# Add the top-side inputs and outputs from the middle outwards.
501
509
for i , (angle , is_input , spec ) \
502
- in enumerate (zip (angles , are_inputs , zip (scaled_flows , pathlengths ))):
510
+ in enumerate (zip (angles , are_inputs ,
511
+ zip (scaled_flows , pathlengths ))):
503
512
if angle == DOWN and is_input :
504
- tips [i ,:], label_locations [i ,:] = self ._add_input (ulpath , angle , * spec )
513
+ tips [i ,:], label_locations [i ,:] = self ._add_input (ulpath , angle ,
514
+ * spec )
505
515
elif angle == UP and not is_input :
506
- tips [i ,:], label_locations [i ,:] = self ._add_output (urpath , angle , * spec )
516
+ tips [i ,:], label_locations [i ,:] = self ._add_output (urpath ,
517
+ angle , * spec )
507
518
# Add the bottom-side inputs and outputs from the middle outwards.
508
519
for i , (angle , is_input , spec ) \
509
- in enumerate (zip (angles , are_inputs , zip (scaled_flows , pathlengths ))[::- 1 ]):
520
+ in enumerate (zip (angles , are_inputs ,
521
+ zip (scaled_flows , pathlengths ))[::- 1 ]):
510
522
if angle == UP and is_input :
511
- tips [n - i - 1 ,:], label_locations [n - i - 1 ,:] = self ._add_input (llpath , angle , * spec )
523
+ (tips [n - i - 1 ,:],
524
+ label_locations [n - i - 1 ,:]) = self ._add_input (llpath , angle ,
525
+ * spec )
512
526
elif angle == DOWN and not is_input :
513
- tips [n - i - 1 ,:], label_locations [n - i - 1 ,:] = self ._add_output (lrpath , angle , * spec )
527
+ (tips [n - i - 1 ,:],
528
+ label_locations [n - i - 1 ,:]) = self ._add_output (lrpath , angle ,
529
+ * spec )
514
530
# Add the left-side inputs from the bottom upwards.
515
531
has_left_input = False
516
532
for i , (angle , is_input , spec ) \
517
- in enumerate (zip (angles , are_inputs , zip (scaled_flows , pathlengths ))[::- 1 ]):
533
+ in enumerate (zip (angles , are_inputs ,
534
+ zip (scaled_flows , pathlengths ))[::- 1 ]):
518
535
if angle == RIGHT and is_input :
519
536
if not has_left_input :
520
- # Make sure the lower path extends at least as far as the upper one.
537
+ # Make sure the lower path extends
538
+ # at least as far as the upper one.
521
539
if llpath [- 1 ][1 ][0 ] > ulpath [- 1 ][1 ][0 ]:
522
- llpath .append ((Path .LINETO , [ulpath [- 1 ][1 ][0 ], llpath [- 1 ][1 ][1 ]]))
540
+ llpath .append ((Path .LINETO , [ulpath [- 1 ][1 ][0 ],
541
+ llpath [- 1 ][1 ][1 ]]))
523
542
has_left_input = True
524
- tips [n - i - 1 ,:], label_locations [n - i - 1 ,:] = self ._add_input (llpath , angle , * spec )
543
+ (tips [n - i - 1 ,:],
544
+ label_locations [n - i - 1 ,:]) = self ._add_input (llpath , angle ,
545
+ * spec )
525
546
# Add the right-side outputs from the top downwards.
526
547
has_right_output = False
527
548
for i , (angle , is_input , spec ) \
528
- in enumerate (zip (angles , are_inputs , zip (scaled_flows , pathlengths ))):
549
+ in enumerate (zip (angles , are_inputs ,
550
+ zip (scaled_flows , pathlengths ))):
529
551
if angle == RIGHT and not is_input :
530
552
if not has_right_output :
531
- # Make sure the upper path extends at least as far as the lower one.
553
+ # Make sure the upper path extends
554
+ # at least as far as the lower one.
532
555
if urpath [- 1 ][1 ][0 ] < lrpath [- 1 ][1 ][0 ]:
533
- urpath .append ((Path .LINETO , [lrpath [- 1 ][1 ][0 ], urpath [- 1 ][1 ][1 ]]))
556
+ urpath .append ((Path .LINETO , [lrpath [- 1 ][1 ][0 ],
557
+ urpath [- 1 ][1 ][1 ]]))
534
558
has_right_output = True
535
- tips [i ,:], label_locations [i ,:] = self ._add_output (urpath , angle , * spec )
559
+ (tips [i ,:],
560
+ label_locations [i ,:]) = self ._add_output (urpath , angle , * spec )
536
561
# Trim any hanging vertices.
537
562
if not has_left_input :
538
563
ulpath .pop ()
@@ -542,8 +567,8 @@ def add(self, patchlabel='', flows=np.array([1.0,-1.0]), orientations=[0,0],
542
567
urpath .pop ()
543
568
544
569
# Concatenate the subpaths in the correct order (clockwise from top).
545
- path = (urpath + self ._revert (lrpath ) + llpath + self ._revert (ulpath )
546
- + [(Path .CLOSEPOLY , urpath [0 ][1 ])])
570
+ path = (urpath + self ._revert (lrpath ) + llpath + self ._revert (ulpath ) +
571
+ [(Path .CLOSEPOLY , urpath [0 ][1 ])])
547
572
548
573
# Create a patch with the Sankey outline.
549
574
codes , vertices = zip (* path )
@@ -561,7 +586,8 @@ def _get_angle(a, r):
561
586
vertices = rotate (vertices )
562
587
text = self .ax .text (0 , 0 , s = patchlabel , ha = 'center' , va = 'center' )
563
588
else :
564
- rotation = self .diagrams [prior ].angles [connect [0 ]] - angles [connect [1 ]]
589
+ rotation = (self .diagrams [prior ].angles [connect [0 ]] -
590
+ angles [connect [1 ]])
565
591
angles = [_get_angle (angle , rotation ) for angle in angles ]
566
592
rotate = Affine2D ().rotate_deg (rotation * 90 ).transform_point
567
593
tips = rotate (tips )
@@ -580,8 +606,8 @@ def _get_angle(a, r):
580
606
xs , ys = zip (* vertices )
581
607
self .ax .plot (xs , ys , 'go-' )
582
608
patch = PathPatch (Path (vertices , codes ),
583
- fc = kwargs .pop ('fc' , kwargs .pop ('facecolor' , # Custom defaults
584
- '#bfd1d4' )),
609
+ fc = kwargs .pop ('fc' , kwargs .pop ('facecolor' ,
610
+ '#bfd1d4' )), # Custom defaults
585
611
lw = kwargs .pop ('lw' , kwargs .pop ('linewidth' ,
586
612
'0.5' )),
587
613
** kwargs )
@@ -592,7 +618,7 @@ def _get_angle(a, r):
592
618
if labels [i ] is None or angle is None :
593
619
labels [i ] = ''
594
620
elif self .unit is not None :
595
- quantity = self .format % abs (number ) + self .unit
621
+ quantity = self .format % abs (number ) + self .unit
596
622
if labels [i ] != '' :
597
623
labels [i ] += "\n "
598
624
labels [i ] += quantity
@@ -608,10 +634,14 @@ def _get_angle(a, r):
608
634
# user wants to provide labels later.
609
635
610
636
# Expand the size of the diagram if necessary.
611
- self .extent = (min (np .min (vertices [:,0 ]), np .min (label_locations [:,0 ]), self .extent [0 ]),
612
- max (np .max (vertices [:,0 ]), np .max (label_locations [:,0 ]), self .extent [1 ]),
613
- min (np .min (vertices [:,1 ]), np .min (label_locations [:,1 ]), self .extent [2 ]),
614
- max (np .max (vertices [:,1 ]), np .max (label_locations [:,1 ]), self .extent [3 ]))
637
+ self .extent = (min (np .min (vertices [:,0 ]), np .min (label_locations [:,0 ]),
638
+ self .extent [0 ]),
639
+ max (np .max (vertices [:,0 ]), np .max (label_locations [:,0 ]),
640
+ self .extent [1 ]),
641
+ min (np .min (vertices [:,1 ]), np .min (label_locations [:,1 ]),
642
+ self .extent [2 ]),
643
+ max (np .max (vertices [:,1 ]), np .max (label_locations [:,1 ]),
644
+ self .extent [3 ]))
615
645
# Include both vertices _and_ label locations in the extents; there are
616
646
# where either could determine the margins (e.g., arrow shoulders).
617
647
@@ -750,7 +780,7 @@ def __init__(self, ax=None, scale=1.0, unit='', format='%G', gap=0.25,
750
780
751
781
**Examples:**
752
782
753
- .. plot:: mpl_examples/api/sankey_demo .py
783
+ .. plot:: mpl_examples/api/sankey_demo_basics .py
754
784
"""
755
785
# Check the arguments.
756
786
assert gap >= 0 , ("The gap is negative.\n This isn't allowed because it "
0 commit comments