Sankey with Matplotlib

风流意气都作罢 提交于 2019-12-11 07:38:46

问题


In this Senkey are two Inputs: K and S, three Outputs: H,F and Sp and the Rest: x

The Inputs shall come from the left Side, the Outputs go to the right Side. The Rest shall go to the Top.

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

fig = plt.figure(figsize = [10,10])
ax = fig.add_subplot(1,1,1)
ax.set(yticklabels=[],xticklabels=[])
ax.text(-10,10, "xxx")

Sankey(ax=ax,  flows = [ 20400,3000,-19900,-400,-2300,800],
              labels = ['K',   'S', 'H',    'F', 'Sp', 'x'],
        orientations = [ 1,    -1,   1,      0,  -1,  -1 ],
scale=1, margin=100,  trunklength=1.0).finish()

plt.tight_layout() 
plt.show()

I played a lot with the orientations, but nothing works or looks nice. And, it there a way to set different colors for every arrow?


回答1:


The scale of the Sankey should be such that input-flow times scale is about 1.0 and output-flow times scale is about -1.0 (see docs). Therefore, about 1/25000 is a good starting point for experimentation. The margin should be a small number, maybe around 1, or leave it out. I think the only way to have individual colors, is to chain multiple Sankeys together (with add), but that's probably not what you want. Use plt.axis("off") to suppress the axes completely.

My test code:

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

fig = plt.figure(figsize = [10,10])
ax = fig.add_subplot(1,1,1)

Sankey(ax=ax,  flows = [ 20400,3000,-19900,-400,-2300,-800],
              labels = ['K',   'S', 'H',   'F', 'Sp',  'x'],
        orientations = [ 1,    -1,   1,     0,   -1,   -1 ],
        scale=1/25000, trunklength=1,
        edgecolor = '#099368', facecolor = '#099368'
      ).finish()
plt.axis("off")
plt.show()

Generated Sankey:




回答2:


With different Colors

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

plt.rc('font', family = 'serif')
plt.rcParams['font.size']  = 10
plt.rcParams['font.serif'] = "Linux Libertine"

fig    = plt.figure(figsize = [6,4], dpi = 330)
ax     = fig.add_subplot(1, 1, 1,)
s      = Sankey(ax = ax, scale = 1/40000, unit = 'kg', gap = .4, shoulder = 0.05,)

s.add(
    flows        = [3000,     20700,   -23700,], 
    orientations = [ 1,       1,       0, ],
    labels       = ["S Value", "K Value", None, ],
    trunklength = 1, pathlengths = 0.4, edgecolor = '#000000', facecolor = 'darkgreen', 
    lw = 0.5,
)

s.add(
    flows        = [23700,  -800,  -2400,     -20500],
    orientations = [0,       1,    -1,         0], 
    labels       = [None,   "U Value", "Sp Value",  None],
    trunklength=1.5, pathlengths=0.5, edgecolor = '#000000', facecolor = 'grey',
    prior = 0, connect = (2,0), lw = 0.5,
    )

s.add(
    flows        = [20500,  -20000,    -500],
    orientations = [0,      -1,        -1],
    labels       = [None,   "H Value",  "F Value"],
    trunklength =1, pathlengths = 0.5, edgecolor = '#000000', facecolor = 'darkred',
    prior = 1, connect = (3,0), lw = 0.5,
)

diagrams = s.finish() 

for d in diagrams:
    for t in d.texts:
        t.set_horizontalalignment('left')


diagrams[0].texts[0].set_position(xy = [-0.58,  0.9,]) # S 
diagrams[0].texts[1].set_position(xy = [-1.5,   0.9,]) # K 
diagrams[2].texts[1].set_position(xy = [ 2.35, -1.2,]) # H 
diagrams[2].texts[2].set_position(xy = [ 1.75, -1.2,]) # F 
diagrams[1].texts[2].set_position(xy = [ 0.7,  -1.2])  # Sp
diagrams[1].texts[1].set_position(xy = [ 0.7,   0.9,]) # U 

# print(diagrams[0].texts[0])
# print(diagrams[0].texts[1])
# print(diagrams[1].texts[0])
# print(diagrams[1].texts[1])
# print(diagrams[1].texts[2])
# print(diagrams[2].texts[0])
# print(diagrams[2].texts[1]) 
# print(diagrams[2].texts[2])

plt.axis("off") 
plt.show()



来源:https://stackoverflow.com/questions/58895137/sankey-with-matplotlib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!