|
| 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() |
0 commit comments