1

I have a (finite) list of trajectories that originate at START position on some 'n' x 'm' grid. I also have a list of counts that represent the number of times each trajectory was traversed. I want to use the Sankey flow diagram to represent the trajectories traversed on a grid. The width of each branch should correspond to the number of times that particular branch was traversed.

I know I can use a Sankey diagram to do this but I don't know how to overlay such a Sankey diagram on the grid and show it as a continuous trajectory (that visits multiple cells on the grid).

[Edit]: Feel free to suggest any other library or tool that I can use for this purpose.

I want my final diagram to show something like this: enter image description here

zoozoo
  • 145
  • 9
  • You 'll probably need to tweak trunklengths and pathlengths. To do it more or less automatic, the approach of [this post](https://stackoverflow.com/questions/51650775/connecting-more-than-two-systems-in-a-sankey-diagram-using-matplotlib-gives-me-m) seems interesting. Creating the code can take some time. Another highly tweaked sankey example: [sankey rankine](https://matplotlib.org/gallery/specialty_plots/sankey_rankine.html) – JohanC Jul 05 '20 at 14:24
  • @JohanC Thanks for sharing the references. Reading them I now understand how to draw multiple Sankey diagrams, but I am still not able to overlay them on a grid properly. – zoozoo Jul 05 '20 at 17:25

1 Answers1

2

Experimenting with the sankey rankine example, the following code is a start to create a grid of sankey diagrams similar to the image in the question.

The left subplot uses default colors (which eases fiddling with the lengths) while the right image shows everything in orange.

import matplotlib.pyplot as plt
from matplotlib.sankey import Sankey

sns.set_style('whitegrid')
fig, axes = plt.subplots(ncols=2, figsize=(11, 4))
for ax in axes:
    ax.set_xticks([k for k in range(-3, 7, 2)])
    ax.set_yticks([k for k in range(-2, 7, 2)])
    ax.grid(True)
    sankey = Sankey(ax=ax, unit=None, scale=1)
    if ax == axes[0]:
        facecolor = None
        edgecolor = None
    else:
        facecolor = 'orange'
        edgecolor = 'orange'
    sankey.add(flows=[1, -.6, -.4], label='4,4', trunklength=1.5, pathlengths=[1, .5, .5],
               orientations=[0, 1, -1], rotation=90, fc=facecolor, ec=edgecolor)
    sankey.add(flows=[.6, -.6], label='4,3', trunklength=.4, pathlengths=[.5, .65],
               orientations=[0, -1], prior=0, connect=(1, 0), fc=facecolor, ec=edgecolor)
    sankey.add(flows=[.6, -.1, -.5], label='3,3', trunklength=.65, pathlengths=[.5, .75, .9],
               orientations=[0, 1, 0], prior=1, connect=(1, 0), fc=facecolor, ec=edgecolor)
    sankey.add(flows=[.4, -.4], label='3,5', trunklength=.55, pathlengths=[.5, .85],
               orientations=[0, 1], prior=0, connect=(2, 0), fc=facecolor, ec=edgecolor)
    sankey.add(flows=[.4, -.2, -.2], label='2,5', trunklength=.65, pathlengths=[.5, 1.75, 1.8],
               orientations=[0, 1, -1], prior=3, connect=(1, 0), fc=facecolor, ec=edgecolor)
    diagrams = sankey.finish()
plt.tight_layout()
plt.show()

example plot

JohanC
  • 71,591
  • 8
  • 33
  • 66