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

Skip to content

Commit 11b6075

Browse files
committed
Merge pull request matplotlib#485 from kdavies4/sankey3
sankey docstring and user doc updated including whats_new
2 parents a3ccb0a + 39c7b55 commit 11b6075

File tree

7 files changed

+531
-685
lines changed

7 files changed

+531
-685
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'.

doc/users/whats_new.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ This page just covers the highlights -- for the full story, see the
1717
new in matplotlib-1.1
1818
=====================
1919

20+
Sankey Diagrams
21+
---------------
22+
23+
Kevin Davies has extended Yannick Copin's original Sankey example into a module
24+
(:mod:`~matplotlib.sankey`) and provided new examples
25+
(:ref:`api-sankey_demo_basics`, :ref:`api-sankey_demo_links`,
26+
:ref:`api-sankey_demo_rankine`).
27+
28+
.. plot:: mpl_examples/api/sankey_demo_rankine.py
29+
2030
Animation
2131
---------
2232

examples/api/sankey_demo.py

Lines changed: 0 additions & 211 deletions
This file was deleted.

examples/api/sankey_demo_basics.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""Demonstrate the Sankey class by producing three basic diagrams.
2+
"""
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
from matplotlib.sankey import Sankey
7+
8+
9+
# Example 1 -- Mostly defaults
10+
# This demonstrates how to create a simple diagram by implicitly calling the
11+
# Sankey.add() method and by appending finish() to the call to the class.
12+
Sankey(flows=[0.25, 0.15, 0.60, -0.20, -0.15, -0.05, -0.50, -0.10],
13+
labels=['', '', '', 'First', 'Second', 'Third', 'Fourth', 'Fifth'],
14+
orientations=[-1, 1, 0, 1, 1, 1, 0, -1]).finish()
15+
plt.title("The default settings produce a diagram like this.")
16+
# Notice:
17+
# 1. Axes weren't provided when Sankey() was instantiated, so they were
18+
# created automatically.
19+
# 2. The scale argument wasn't necessary since the data was already
20+
# normalized.
21+
# 3. By default, the lengths of the paths are justified.
22+
23+
# Example 2
24+
# This demonstrates:
25+
# 1. Setting one path longer than the others
26+
# 2. Placing a label in the middle of the diagram
27+
# 3. Using the the scale argument to normalize the flows
28+
# 4. Implicitly passing keyword arguments to PathPatch()
29+
# 5. Changing the angle of the arrow heads
30+
# 6. Changing the offset between the tips of the paths and their labels
31+
# 7. Formatting the numbers in the path labels and the associated unit
32+
# 8. Changing the appearance of the patch and the labels after the figure is
33+
# created
34+
fig = plt.figure()
35+
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
36+
title="Flow Diagram of a Widget")
37+
sankey = Sankey(ax=ax, scale=0.01, offset=0.2, head_angle=180,
38+
format='%.0f', unit='%')
39+
sankey.add(flows=[25, 0, 60, -10, -20, -5, -15, -10, -40],
40+
labels = ['', '', '', 'First', 'Second', 'Third', 'Fourth',
41+
'Fifth', 'Hurray!'],
42+
orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0],
43+
pathlengths = [0.25, 0.25, 0.25, 0.25, 0.25, 0.6, 0.25, 0.25,
44+
0.25],
45+
patchlabel="Widget\nA",
46+
alpha=0.2, lw=2.0) # Arguments to matplotlib.patches.PathPatch()
47+
diagrams = sankey.finish()
48+
diagrams[0].patch.set_facecolor('#37c959')
49+
diagrams[0].texts[-1].set_color('r')
50+
diagrams[0].text.set_fontweight('bold')
51+
# Notice:
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.
57+
58+
# Example 3
59+
# This demonstrates:
60+
# 1. Connecting two systems
61+
# 2. Turning off the labels of the quantities
62+
# 3. Adding a legend
63+
fig = plt.figure()
64+
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[], title="Two Systems")
65+
flows = [0.25, 0.15, 0.60, -0.10, -0.05, -0.25, -0.15, -0.10, -0.35]
66+
sankey = Sankey(ax=ax, unit=None)
67+
sankey.add(flows=flows, label='one',
68+
orientations=[-1, 1, 0, 1, 1, 1, -1, -1, 0])
69+
sankey.add(flows=[-0.25, 0.15, 0.1], fc='#37c959', label='two',
70+
orientations=[-1, -1, -1], prior=0, connect=(0, 0))
71+
diagrams = sankey.finish()
72+
diagrams[-1].patch.set_hatch('/')
73+
plt.legend(loc='best')
74+
# Notice that only one connection is specified, but the systems form a
75+
# circuit since: (1) the lengths of the paths are justified and (2) the
76+
# orientation and ordering of the flows is mirrored.
77+
78+
plt.show()

examples/api/sankey_demo_links.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
"""Demonstrate/test the Sankey class by producing a long chain of connections.
2+
"""
3+
import numpy as np
4+
import matplotlib.pyplot as plt
5+
6+
from matplotlib.sankey import Sankey
7+
from itertools import cycle
8+
9+
links_per_side = 6
10+
def side(sankey, n=1):
11+
"""Generate a side chain.
12+
"""
13+
prior = len(sankey.diagrams)
14+
colors = cycle(['orange', 'b', 'g', 'r', 'c', 'm', 'y'])
15+
for i in range(0, 2*n, 2):
16+
sankey.add(flows=[1, -1], orientations=[-1, -1],
17+
patchlabel=str(prior+i), facecolor=colors.next(),
18+
prior=prior+i-1, connect=(1, 0), alpha=0.5)
19+
sankey.add(flows=[1, -1], orientations=[1, 1],
20+
patchlabel=str(prior+i+1), facecolor=colors.next(),
21+
prior=prior+i, connect=(1, 0), alpha=0.5)
22+
def corner(sankey):
23+
"""Generate a corner link.
24+
"""
25+
prior = len(sankey.diagrams)
26+
sankey.add(flows=[1, -1], orientations=[0, 1],
27+
patchlabel=str(prior), facecolor='k',
28+
prior=prior-1, connect=(1, 0), alpha=0.5)
29+
fig = plt.figure()
30+
ax = fig.add_subplot(1, 1, 1, xticks=[], yticks=[],
31+
title="Why would you want to do this?\n(But you could.)")
32+
sankey = Sankey(ax=ax, unit=None)
33+
sankey.add(flows=[1, -1], orientations=[0, 1],
34+
patchlabel="0", facecolor='k',
35+
rotation=45)
36+
side(sankey, n=links_per_side)
37+
corner(sankey)
38+
side(sankey, n=links_per_side)
39+
corner(sankey)
40+
side(sankey, n=links_per_side)
41+
corner(sankey)
42+
side(sankey, n=links_per_side)
43+
sankey.finish()
44+
# Notice:
45+
# 1. The alignment doesn't drift significantly (if at all; with 16007
46+
# subdiagrams there is still closure).
47+
# 2. The first diagram is rotated 45 deg, so all other diagrams are rotated
48+
# accordingly.
49+
50+
plt.show()

0 commit comments

Comments
 (0)