python - Matplotlib 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
andunit
参数,以防止再次添加百分比。
由于 sankey 仍在编写空格式和单位,因此可以更新文本以删除空行并获得正确的垂直居中。
from matplotlib import pyplot as plt
from matplotlib.sankey import Sankey
from random import randint
Input = 240.1
L0 = 140.1
F9 = 21.1
L = [randint(1,300) for _ in range(9)]
norm_factor = (Input - L0 - F9) / sum(L)
flows_s2 = [Input - L0] + [-l * norm_factor for l in L] + [-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:
text = t.get_text()
if text[-1] == '\n': # remove empty line at the end, needed for centering
t.set_text(text[:-1])
t.set_fontsize(10)
t.set_verticalalignment('center')
if text[:4] == 'Loss' and text[:6] != 'Loss 0': # align all loss labels except loss 0
t.set_horizontalalignment('left')
xy = t.get_position()
t.set_position(xy=(0.18, xy[1]))
else:
t.set_horizontalalignment('center')
#t.set_bbox(dict(facecolor='red', alpha=0.5, edgecolor='blue'))
diagrams[0].texts[0].set_position(xy=(0, 0.42)) # adjust position of input 1
diagrams[0].texts[1].set_position(xy=(1.75, diagrams[0].texts[1].get_position()[1])) # adjust pos. of loss 0
diagrams[0].texts[2].set_text('') # remove output 1 as it coincides with input 2
diagrams[1].texts[-1].set_position(xy=(diagrams[1].texts[-1].get_position()[0], -5)) # adjust pos. of output
plt.tight_layout()
plt.show()
推荐阅读
- ibm-mq - 运行 IBM MQ dmpmqcfg 导致“libmqds_r.dylib”错误
- python - 如何使用 pySpark 比较两个 CSV 文件并验证是否存在
- c++ - OpenCV YAML:如何读写嵌套(递归)yaml文件
- sublimetext3 - Sublime text 分组打开文件夹中的文件
- angular - 检查请求是否已经按承诺完成
- html - 如何将导航栏上的菜单按钮定位为最右边的项目
- git - 如何在 Azure 管道中标记存储库?您需要 Git 'GenericContribute' 权限才能执行此操作
- javascript - 当使用 .map 并且索引内的输入标签始终为 0
- apache-flink - Flink disableOperatorChaining 性能影响
- python - 如果一列中的元素被看到两次,pandas 将数据帧分成两部分