You are reading an old version of the documentation (v3.1.1). For the latest version see
Version 3.1.2
Fork me on GitHub


Module for creating Sankey diagrams using Matplotlib.

class matplotlib.sankey.Sankey(ax=None, scale=1.0, unit='', format='%G', gap=0.25, radius=0.1, shoulder=0.03, offset=0.15, head_angle=100, margin=0.4, tolerance=1e-06, **kwargs)[source]

Bases: object

Sankey diagram.

Sankey diagrams are a specific type of flow diagram, in which the width of the arrows is shown proportionally to the flow quantity. They are typically used to visualize energy or material or cost transfers between processes. Wikipedia (6/1/2011)

Create a new Sankey instance.

Optional keyword arguments:

Field Description
ax axes onto which the data should be plotted If ax isn't provided, new axes will be created.
scale scaling factor for the flows scale sizes the width of the paths in order to maintain proper layout. The same scale is applied to all subdiagrams. The value should be chosen such that the product of the scale and the sum of the inputs is approximately 1.0 (and the product of the scale and the sum of the outputs is approximately -1.0).
unit string representing the physical unit associated with the flow quantities If unit is None, then none of the quantities are labeled.
format a Python number formatting string to be used in labeling the flow as a quantity (i.e., a number times a unit, where the unit is given)
gap space between paths that break in/break away to/from the top or bottom
radius inner radius of the vertical paths
shoulder size of the shoulders of output arrowS
offset text offset (from the dip or tip of the arrow)
head_angle angle of the arrow heads (and negative of the angle of the tails) [deg]
margin minimum space between Sankey outlines and the edge of the plot area
tolerance acceptable maximum of the magnitude of the sum of flows The magnitude of the sum of connected flows cannot be greater than tolerance.

The optional arguments listed above are applied to all subdiagrams so that there is consistent alignment and formatting.

If Sankey is instantiated with any keyword arguments other than those explicitly listed above (**kwargs), they will be passed to add(), which will create the first subdiagram.

In order to draw a complex Sankey diagram, create an instance of Sankey by calling it without any kwargs:

sankey = Sankey()

Then add simple Sankey sub-diagrams:

sankey.add() # 1
sankey.add() # 2
sankey.add() # n

Finally, create the full diagram:


Or, instead, simply daisy-chain those calls:

Sankey().add().add...  .add().finish()


(Source code)


(png, pdf)


(png, pdf)


(png, pdf)

add(self, patchlabel='', flows=None, orientations=None, labels='', trunklength=1.0, pathlengths=0.25, prior=None, connect=(0, 0), rotation=0, **kwargs)[source]

Add a simple Sankey diagram with flows at the same hierarchical level.

patchlabel : str

Label to be placed at the center of the diagram. Note that label (not patchlabel) can be passed as keyword argument to create an entry in the legend.

flows : list of float

Array of flow values. By convention, inputs are positive and outputs are negative.

Flows are placed along the top of the diagram from the inside out in order of their index within flows. They are placed along the sides of the diagram from the top down and along the bottom from the outside in.

If the sum of the inputs and outputs is nonzero, the discrepancy will appear as a cubic Bezier curve along the top and bottom edges of the trunk.

orientations : list of {-1, 0, 1}

List of orientations of the flows (or a single orientation to be used for all flows). Valid values are 0 (inputs from the left, outputs to the right), 1 (from and to the top) or -1 (from and to the bottom).

labels : list of (str or None)

List of labels for the flows (or a single label to be used for all flows). Each label may be None (no label), or a labeling string. If an entry is a (possibly empty) string, then the quantity for the corresponding flow will be shown below the string. However, if the unit of the main diagram is None, then quantities are never shown, regardless of the value of this argument.

trunklength : float

Length between the bases of the input and output groups (in data-space units).

pathlengths : list of float

List of lengths of the vertical arrows before break-in or after break-away. If a single value is given, then it will be applied to the first (inside) paths on the top and bottom, and the length of all other arrows will be justified accordingly. The pathlengths are not applied to the horizontal inputs and outputs.

prior : int

Index of the prior diagram to which this diagram should be connected.

connect : (int, int)

A (prior, this) tuple indexing the flow of the prior diagram and the flow of this diagram which should be connected. If this is the first diagram or prior is None, connect will be ignored.

rotation : float

Angle of rotation of the diagram in degrees. The interpretation of the orientations argument will be rotated accordingly (e.g., if rotation == 90, an orientations entry of 1 means to/from the left). rotation is ignored if this diagram is connected to an existing one (using prior and connect).


The current Sankey instance.

Other Parameters:

Additional keyword arguments set matplotlib.patches.PathPatch properties, listed below. For example, one may want to use fill=False or label="A legend entry".

Property Description
agg_filter a filter function, which takes a (m, n, 3) float array and a dpi value, and returns a (m, n, 3) array
alpha float or None
animated bool
antialiased or aa unknown
capstyle {'butt', 'round', 'projecting'}
clip_box Bbox
clip_on bool
clip_path [(Path, Transform) | Patch | None]
color color
contains callable
edgecolor or ec color or None or 'auto'
facecolor or fc color or None
figure Figure
fill bool
gid str
hatch {'/', '\', '|', '-', '+', 'x', 'o', 'O', '.', '*'}
in_layout bool
joinstyle {'miter', 'round', 'bevel'}
label object
linestyle or ls {'-', '--', '-.', ':', '', (offset, on-off-seq), ...}
linewidth or lw float or None
path_effects AbstractPathEffect
picker None or bool or float or callable
rasterized bool or None
sketch_params (scale: float, length: float, randomness: float)
snap bool or None
transform Transform
url str
visible bool
zorder float

See also


Adjust the axes and return a list of information about the Sankey subdiagram(s).

Return value is a list of subdiagrams represented with the following fields:

Field Description
patch Sankey outline (an instance of PathPatch)
flows values of the flows (positive for input, negative for output)
angles list of angles of the arrows [deg/90] For example, if the diagram has not been rotated, an input to the top side will have an angle of 3 (DOWN), and an output from the top side will have an angle of 1 (UP). If a flow has been skipped (because its magnitude is less than tolerance), then its angle will be None.
tips array in which each row is an [x, y] pair indicating the positions of the tips (or "dips") of the flow paths If the magnitude of a flow is less the tolerance for the instance of Sankey, the flow is skipped and its tip will be at the center of the diagram.
text Text instance for the label of the diagram
texts list of Text instances for the labels of flows

See also