我正在制作一个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()
桑基图
假设您的输入值是显示的百分比,您可以通过手动将该信息添加到每个标签来实现目标。需要新行时使用
'\n'
,在一行上写入百分比时使用短划线' – '
或冒号。您需要重置sankey的format
和unit
参数,以防止再次添加百分比由于sankey仍在写入空格式和单位,因此可以更新文本以删除空行并获得正确的垂直居中
相关问题 更多 >
编程相关推荐