[Python] Diagram of road intersection flows (Sankey or other)

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a road intersection flow diagram in Python. The problem is, that none of the Sankey-specific modules that I managed to find support setting coordinates of the nodes and rotation of output and input flows. For example, this is what I achieved with sugikey library:

    import networkx as nx
    import matplotlib.pyplot as plt
    import sugikey

    coords = {
    'West': {
    'out': (0, 95),
    'in': (0, 105)
    },
    'South': {
    'out': (105, 0),
    'in': (95, 0)
    },
    'East': {
    'out': (200, 105),
    'in': (200, 95)
    },
    'North': {
    'out': (95, 200),
    'in': (105, 200)
    }
    }

    flows = {
    'West': {
    'South': 5,
    'East': 15,
    'North': 3
    },
    'South': {
    'West': 5,
    'East': 2,
    'North': 13
    },
    'East': {
    'South': 3,
    'West': 17,
    'North': 7
    },
    'North': {
    'South': 11,
    'East': 2,
    'West': 1
    }
    }

    dig = nx.DiGraph()
    for out_node, dest_flows in flows.items():
    onode = coords[out_node]['out']
    for in_node, flow in dest_flows.items():
    inode = coords[in_node]['in']
    if onode not in dig.nodes:
    dig.add_node(onode, name=out_node)
    if inode not in dig.nodes:
    dig.add_node(inode, name=in_node)
    dig.add_edge(onode, inode, value=flow)

    fig, ax = plt.subplots()
    sugikey.sankey.sankey_from_dig(dig, ax=ax)
    fig.savefig("test.png")


    [​IMG]

    The idea is correct, but it needs to look something like this (from this site):

    [​IMG]

    In my case there can be 2 or more nodes with in/out flows (some will be only in or out, e.g. one-way roads), coordinates are arbitrary, but are more or less equally far from the intersection center. Currently I use my own solution (non-Sankey, based on pure trigonometry and bezier module to connect groups), which works too, but the code became too messy and results are inconsistent, sometimes rather ugly or incorrect.


    1. Relatively good example, but road relation F to E is off. [​IMG]


    2. Not so successful in cases where flows are imbalanced, or the geometry is tricky. [​IMG]

    My question is: is there a way to create a Sankey diagram to serve my use case? Or it's a waste of time to even try? If it is, what would you suggest? I would be grateful for any advice!

    Continue reading...

Compartilhe esta Página