Matplotlib Sankey标签和值

问题描述 投票:0回答:1

我正在处理Sankey图,并且在标注过程中遇到了一些问题。有没有办法将标签和每个流的值放在同一行上?我希望他们看起来像这样:“标签:价值”。

这里是代码和结果图。

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

fig = plt.figure(figsize=(8.3, 11.7))
ax = fig.add_subplot(1, 1, 1)
plt.axis('off')

# these will be provided soon
# Input = ...
# L0 = ...
# L1, L2, L3, L4, L5, L6, L7, L8, L9 = ...
# F9 = ...

sankey = Sankey(ax=ax,
                scale=1 / Input,
                offset=0.6,
                head_angle=135,
                shoulder=0,
                gap=0.2,
                radius=0.1,
                format='%.1f',
                unit='%')
s0 = sankey.add(flows=[Input, -L0, -(Input - L0)],
                labels=['Input 1', 'Loss 0', ''],
                orientations=[0, 1, 0],
                trunklength=1,
                rotation=-90,
                fc='crimson', alpha=0.8)
s1 = sankey.add(flows=[Input - L0, -L1, -L2, -L3, -L4, -L5, -L6, -L7, -L8, -L9, -F9],
                labels=['Input 2', 'Loss 1', 'Loss 2', 'Loss 3', 'Loss 4', 'Loss 5', 'Loss 6', 'Loss 7', 'Loss 8',
                        'Loss 9', 'Output'],
                orientations=[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                trunklength=1,
                rotation=-90,
                prior=0, connect=(2, 0),
                fc='crimson', alpha=0.6)
diagrams = sankey.finish()
for d in diagrams:
    for t in d.texts:
        t.set_fontsize(10)
        t.set_horizontalalignment('center')
        t.set_verticalalignment('center')

diagrams[0].texts[0].set_position(xy=[0, 0.4])
diagrams[0].texts[2].set_position(xy=[10, 10])

plt.show()

Sankey图

“”

python matplotlib label sankey-diagram
1个回答
0
投票

假设您的输入值是显示的百分比,则可以达到以下目标:

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

# some made up values to experiment with:
Input = 240.1
L0 = 140.1
L1, L2, L3, L4, L5, L6, L7, L8, L9 = [5.2] * 9
F9 = 21.1

flows_s2 = [Input - L0, -L1, -L2, -L3, -L4, -L5, -L6, -L7, -L8, -L9, -F9]
labels_s2 = ['Input 2', 'Loss 1', 'Loss 2', 'Loss 3', 'Loss 4', 'Loss 5', 'Loss 6', 'Loss 7', 'Loss 8', 'Loss 9',
             'Output']
labels_s2_long = [f'{label}\n{flow} %' for label, flow in zip(labels_s2[:1], flows_s2)]
labels_s2_long += [f'{label} – {-flow:.1f} %' for label, flow in zip(labels_s2[1:], flows_s2[1:])]

fig = plt.figure(figsize=(8.3, 11.7))
ax = fig.add_subplot(1, 1, 1)
plt.axis('off')
sankey = Sankey(ax=ax,
                scale=2 / Input,
                offset=0.6,
                head_angle=135,
                shoulder=0,
                gap=0.2,
                radius=0.1,
                format='%.1f',
                unit='%')
s0 = sankey.add(flows=[Input, -L0, -(Input - L0)],
                labels=['Input 1', 'Loss 0', ''],
                orientations=[0, 1, 0],
                trunklength=1,
                rotation=-90,
                fc='crimson', alpha=0.8)
sankey.format = ''
sankey.unit = ''
s1 = sankey.add(flows=flows_s2,
                labels=labels_s2_long,
                orientations=[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0],
                trunklength=1,
                rotation=-90,
                prior=0, connect=(2, 0),
                fc='tomato', alpha=0.6)
diagrams = sankey.finish()
for d in diagrams:
    for t in d.texts:
        t.set_fontsize(10)
        t.set_horizontalalignment('center')
        t.set_verticalalignment('center')

diagrams[0].texts[0].set_position(xy=[0, 0.4])
diagrams[0].texts[2].set_position(xy=[10, 10])
© www.soinside.com 2019 - 2024. All rights reserved.