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.

[1]:

import pandas as pd
from io import StringIO

year,source,target,value
2020,A,B,10
2025,A,B,20
"""))

flows

[1]:

year source target value
0 2020 A B 10
1 2025 A B 20
[2]:

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:

[3]:

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

[4]:

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


But in fact they have different scales:

[5]:

w1.scale, w2.scale

[5]:

(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:

[6]:

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])