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

Skip to content

Commit dec14bd

Browse files
committed
Improved documentation and formatting of Sankey class and its demo
1 parent a51010b commit dec14bd

File tree

3 files changed

+338
-515
lines changed

3 files changed

+338
-515
lines changed

doc/api/api_changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ For new features that were added to matplotlib, please see
1414
Changes in 1.1.x
1515
================
1616

17+
* Added new :class:`matplotlib.sankey.Sankey` for generating Sankey diagrams.
18+
1719
* In :meth:`~matplotlib.pyplot.imshow`, setting *interpolation* to 'nearest'
1820
will now always mean that the nearest-neighbor interpolation is performed.
1921
If you want the no-op interpolation to be performed, choose 'none'.

examples/api/sankey_demo.py

Lines changed: 22 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,6 @@
66
from matplotlib.sankey import Sankey
77
from itertools import cycle
88

9-
10-
"""Demonstrate the Sankey class.
11-
"""
12-
import matplotlib.pyplot as plt
13-
from itertools import cycle
14-
159
# Example 1 -- Mostly defaults
1610
# This demonstrates how to create a simple diagram by implicitly calling the
1711
# Sankey.add() method and by appending finish() to the call to the class.
@@ -35,8 +29,8 @@
3529
# 5. Changing the angle of the arrow heads
3630
# 6. Changing the offset between the tips of the paths and their labels
3731
# 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
4034
fig = plt.figure()
4135
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
4236
title="Flow Diagram of a Widget")
@@ -49,20 +43,17 @@
4943
pathlengths = [0.25, 0.25, 0.25, 0.25, 0.25, 0.6, 0.25, 0.25,
5044
0.25],
5145
patchlabel="Widget\nA",
52-
alpha=0.2, lw=2.0) # Arguments to matplotlib.patches.PathPatch()
46+
alpha=0.2, lw=2.0) # Arguments to matplotlib.patches.PathPatch()
5347
diagrams = sankey.finish()
5448
diagrams[0].patch.set_facecolor('#37c959')
5549
diagrams[0].texts[-1].set_color('r')
5650
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')
6151
# 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.
6657

6758
# Example 3
6859
# This demonstrates:
@@ -79,18 +70,17 @@
7970
orientations=[-1, -1, -1], prior=0, connect=(0, 0))
8071
diagrams = sankey.finish()
8172
diagrams[-1].patch.set_hatch('/')
82-
# Without namedtuple:
83-
#diagrams[-1][0].set_hatch('/')
84-
8573
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.
8977

9078
# Example 4
9179
# This tests a long chain of connections.
9280
links_per_side = 6
9381
def side(sankey, n=1):
82+
"""Generate a side chain.
83+
"""
9484
prior = len(sankey.diagrams)
9585
colors = cycle(['orange', 'b', 'g', 'r', 'c', 'm', 'y'])
9686
for i in range(0, 2*n, 2):
@@ -101,14 +91,15 @@ def side(sankey, n=1):
10191
patchlabel=str(prior+i+1), facecolor=colors.next(),
10292
prior=prior+i, connect=(1, 0), alpha=0.5)
10393
def corner(sankey):
94+
"""Generate a corner link.
95+
"""
10496
prior = len(sankey.diagrams)
10597
sankey.add(flows=[1, -1], orientations=[0, 1],
10698
patchlabel=str(prior), facecolor='k',
10799
prior=prior-1, connect=(1, 0), alpha=0.5)
108100
fig = plt.figure()
109101
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.)")
112103
sankey = Sankey(ax=ax, unit=None)
113104
sankey.add(flows=[1, -1], orientations=[0, 1],
114105
patchlabel="0", facecolor='k',
@@ -124,26 +115,20 @@ def corner(sankey):
124115
# Notice:
125116
# 1. The alignment doesn't drift significantly (if at all; with 16007
126117
# 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.
129120

130121
# Example 5
131-
# This demonstrates a practical example -- a Rankine power cycle.
122+
# This is a practical example of a Rankine power cycle.
132123
fig = plt.figure(figsize=(8, 12))
133124
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
134125
title="Rankine Power Cycle: Example 8.6 from Moran and Shapiro\n"
135126
+ "\x22Fundamentals 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,
137128
142.361, 10.193, 10.210, 43.670, 44.312,
138129
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])
147132
sankey.add(patchlabel='\n\nPump 1', rotation=90, facecolor='#37c959',
148133
flows=[Hdot[13], Hdot[6], -Hdot[7]],
149134
labels=['Shaft power', '', None],
@@ -199,10 +184,6 @@ def corner(sankey):
199184
diagram.text.set_fontweight('bold')
200185
diagram.text.set_fontsize('10')
201186
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]:
206187
text.set_fontsize('10')
207188
# Notice that the explicit connections are handled automatically, but the
208189
# implicit ones currently are not. The lengths of the paths and the trunks

0 commit comments

Comments
 (0)