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

Skip to content

Commit 79dce7c

Browse files
committed
Split the demo into 3 files and made only 1 run from whats_new.rst
1 parent 6f11254 commit 79dce7c

File tree

5 files changed

+215
-211
lines changed

5 files changed

+215
-211
lines changed

doc/users/whats_new.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ new in matplotlib-1.1
1818
=====================
1919

2020
Sankey Diagrams
21-
---------
21+
---------------
2222

2323
Kevin Davies has extended Yannick Copin's original Sankey example into a module
24-
(:mod:`~matplotlib.sankey`) and provided new examples (:ref:`api-sankey_demo`).
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`).
2527

26-
.. plot:: mpl_examples/api/sankey_demo.py 5 # Run the 5th example.
28+
.. plot:: mpl_examples/api/sankey_demo_rankine.py
2729

2830
Animation
2931
---------

examples/api/sankey_demo.py

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

examples/api/sankey_demo_basics.py

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

examples/api/sankey_demo_links.py

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

0 commit comments

Comments
 (0)