6
6
from matplotlib .sankey import Sankey
7
7
from itertools import cycle
8
8
9
-
10
- """Demonstrate the Sankey class.
11
- """
12
- import matplotlib .pyplot as plt
13
- from itertools import cycle
14
-
15
9
# Example 1 -- Mostly defaults
16
10
# This demonstrates how to create a simple diagram by implicitly calling the
17
11
# Sankey.add() method and by appending finish() to the call to the class.
35
29
# 5. Changing the angle of the arrow heads
36
30
# 6. Changing the offset between the tips of the paths and their labels
37
31
# 7. Formatting the numbers in the path labels and the associated unit
38
- # 8. Changing the appearance of the patch and the labels after the figure
39
- # is created
32
+ # 8. Changing the appearance of the patch and the labels after the figure is
33
+ # created
40
34
fig = plt .figure ()
41
35
ax = fig .add_subplot (1 , 1 , 1 , xticks = [], yticks = [],
42
36
title = "Flow Diagram of a Widget" )
49
43
pathlengths = [0.25 , 0.25 , 0.25 , 0.25 , 0.25 , 0.6 , 0.25 , 0.25 ,
50
44
0.25 ],
51
45
patchlabel = "Widget\n A" ,
52
- alpha = 0.2 , lw = 2.0 ) # Arguments to matplotlib.patches.PathPatch()
46
+ alpha = 0.2 , lw = 2.0 ) # Arguments to matplotlib.patches.PathPatch()
53
47
diagrams = sankey .finish ()
54
48
diagrams [0 ].patch .set_facecolor ('#37c959' )
55
49
diagrams [0 ].texts [- 1 ].set_color ('r' )
56
50
diagrams [0 ].text .set_fontweight ('bold' )
57
- # Without namedtuple:
58
- #diagrams[0][0].set_facecolor('#37c959')
59
- #diagrams[0][5][-1].set_color('r')
60
- #diagrams[0][4].set_fontweight('bold')
61
51
# Notice:
62
- # 1. Since the sum of the flows isn't zero, the width of the trunk isn't
63
- # uniform. A message is given in the terminal window.
64
- # 2. The second flow doesn't appear because its value is zero. A messsage
65
- # is given in the terminal window.
52
+ # 1. Since the sum of the flows is nonzero, the width of the trunk isn't
53
+ # uniform. If verbose.level is helpful (in matplotlibrc), a message is
54
+ # given in the terminal window.
55
+ # 2. The second flow doesn't appear because its value is zero. Again, if
56
+ # verbose.level is helpful, a message is given in the terminal window.
66
57
67
58
# Example 3
68
59
# This demonstrates:
79
70
orientations = [- 1 , - 1 , - 1 ], prior = 0 , connect = (0 , 0 ))
80
71
diagrams = sankey .finish ()
81
72
diagrams [- 1 ].patch .set_hatch ('/' )
82
- # Without namedtuple:
83
- #diagrams[-1][0].set_hatch('/')
84
-
85
73
plt .legend (loc = 'best' )
86
- # Notice that only one connection is specified, but the systems form a
87
- # circuit since: (1) the lengths of the paths are justified and (2) the
88
- # orientation and ordering of the flows is mirrored.
74
+ # Notice that only one connection is specified, but the systems form a circuit
75
+ # since: (1) the lengths of the paths are justified and (2) the orientation and
76
+ # ordering of the flows is mirrored.
89
77
90
78
# Example 4
91
79
# This tests a long chain of connections.
92
80
links_per_side = 6
93
81
def side (sankey , n = 1 ):
82
+ """Generate a side chain.
83
+ """
94
84
prior = len (sankey .diagrams )
95
85
colors = cycle (['orange' , 'b' , 'g' , 'r' , 'c' , 'm' , 'y' ])
96
86
for i in range (0 , 2 * n , 2 ):
@@ -101,14 +91,15 @@ def side(sankey, n=1):
101
91
patchlabel = str (prior + i + 1 ), facecolor = colors .next (),
102
92
prior = prior + i , connect = (1 , 0 ), alpha = 0.5 )
103
93
def corner (sankey ):
94
+ """Generate a corner link.
95
+ """
104
96
prior = len (sankey .diagrams )
105
97
sankey .add (flows = [1 , - 1 ], orientations = [0 , 1 ],
106
98
patchlabel = str (prior ), facecolor = 'k' ,
107
99
prior = prior - 1 , connect = (1 , 0 ), alpha = 0.5 )
108
100
fig = plt .figure ()
109
101
ax = fig .add_subplot (1 , 1 , 1 , xticks = [], yticks = [],
110
- title = "Why would you want to do this?" \
111
- "\n (But you could.)" )
102
+ title = "Why would you want to do this?\n (But you could.)" )
112
103
sankey = Sankey (ax = ax , unit = None )
113
104
sankey .add (flows = [1 , - 1 ], orientations = [0 , 1 ],
114
105
patchlabel = "0" , facecolor = 'k' ,
@@ -124,26 +115,20 @@ def corner(sankey):
124
115
# Notice:
125
116
# 1. The alignment doesn't drift significantly (if at all; with 16007
126
117
# subdiagrams there is still closure).
127
- # 2. The first diagram is rotated 45 degrees , so all other diagrams are
128
- # rotated accordingly.
118
+ # 2. The first diagram is rotated 45 deg , so all other diagrams are rotated
119
+ # accordingly.
129
120
130
121
# Example 5
131
- # This demonstrates a practical example -- a Rankine power cycle.
122
+ # This is a practical example of a Rankine power cycle.
132
123
fig = plt .figure (figsize = (8 , 12 ))
133
124
ax = fig .add_subplot (1 , 1 , 1 , xticks = [], yticks = [],
134
125
title = "Rankine Power Cycle: Example 8.6 from Moran and Shapiro\n "
135
126
+ "\x22 Fundamentals of Engineering Thermodynamics\x22 , 6th ed., 2008" )
136
- Hdot = np . array ( [260.431 , 35.078 , 180.794 , 221.115 , 22.700 ,
127
+ Hdot = [260.431 , 35.078 , 180.794 , 221.115 , 22.700 ,
137
128
142.361 , 10.193 , 10.210 , 43.670 , 44.312 ,
138
129
68.631 , 10.758 , 10.758 , 0.017 , 0.642 ,
139
- 232.121 , 44.559 , 100.613 , 132.168 ])* 1.0e6 # W
140
- sankey = Sankey (ax = ax , format = '%.3G' , unit = 'W' , gap = 0.5 , scale = 1.0 / Hdot [0 ])
141
- # Shared copy:
142
- #Hdot = [260.431, 35.078, 180.794, 221.115, 22.700,
143
- # 142.361, 10.193, 10.210, 43.670, 44.312,
144
- # 68.631, 10.758, 10.758, 0.017, 0.642,
145
- # 232.121, 44.559, 100.613, 132.168] # MW
146
- #sankey = Sankey(ax=ax, format='%.3G', unit=' MW', gap=0.5, scale=1.0/Hdot[0])
130
+ 232.121 , 44.559 , 100.613 , 132.168 ] # MW
131
+ sankey = Sankey (ax = ax , format = '%.3G' , unit = ' MW' , gap = 0.5 , scale = 1.0 / Hdot [0 ])
147
132
sankey .add (patchlabel = '\n \n Pump 1' , rotation = 90 , facecolor = '#37c959' ,
148
133
flows = [Hdot [13 ], Hdot [6 ], - Hdot [7 ]],
149
134
labels = ['Shaft power' , '' , None ],
@@ -199,10 +184,6 @@ def corner(sankey):
199
184
diagram .text .set_fontweight ('bold' )
200
185
diagram .text .set_fontsize ('10' )
201
186
for text in diagram .texts :
202
- # Without namedtuple:
203
- #diagram[4].set_fontweight('bold')
204
- #diagram[4].set_fontsize('10')
205
- #for text in diagram[5]:
206
187
text .set_fontsize ('10' )
207
188
# Notice that the explicit connections are handled automatically, but the
208
189
# implicit ones currently are not. The lengths of the paths and the trunks
0 commit comments