Setting the scale

This recipe demonstrates how the scale of the Sankey diagram is set.

By default the scale is calculated for each diagram to achieve a certain whitespace-to-flow ratio within the height that is given. But in some cases, you may want to set the scale explicitly.

For demonstration, the CSV data is written directly in the cell below – in practice you would want to load data a file.

import pandas as pd
from io import StringIO

flows = pd.read_csv(StringIO("""

year source target value
0 2020 A B 10
1 2025 A B 20
from floweaver import *

# Set the default size to fit the documentation better.
size = dict(width=100, height=100,
            margins=dict(left=20, right=20, top=10, bottom=10))

nodes = {
    'A': ProcessGroup(['A']),
    'B': ProcessGroup(['B']),

bundles = [
    Bundle('A', 'B'),

ordering = [['A'], ['B']]

sdd = SankeyDefinition(nodes, bundles, ordering)

If we draw the flow for the year 2020 and the year 2025 separately, they appear the same:

w1 = weave(sdd, flows.query('year == 2020')).to_widget(**size)
w2 = weave(sdd, flows.query('year == 2025')).to_widget(**size)

But in fact they have different scales:

w1.scale, w2.scale
(None, None)

The units of the scale are units-of-value per pixel.

If we draw the Sankeys again while setting the scale, we can see that the flow indeed has changed between years:

SCALE = 2.0

from ipywidgets import HBox

w1 = weave(sdd, flows.query('year == 2020')).to_widget(**size)
w2 = weave(sdd, flows.query('year == 2025')).to_widget(**size)

w1.scale = w2.scale = SCALE

HBox([w1, w2])