在 SankeyFlow 中更改字体大小

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

自从看到关于python中Sankey图的答案,我就想熟悉一下“SankeyFlow”。我在下面使用了完全相同的代码,但想知道是否可以更改文本的字体大小和位置。

from sankeyflow import Sankey
import matplotlib.pyplot as plt

plt.figure(figsize=(20, 10), dpi=144)
flows = [
('Product', 'Total revenue', 20779),
('Sevice\nand other', 'Total revenue', 30949),
('Total revenue', 'Gross margin', 34768),
('Total revenue', 'Cost of revenue', 16960),
('Gross margin', 'Operating income', 22247),
('Operating income', 'Income before\nincome taxes', 22247, {'flow_color_mode': 'dest'}),
('Other income, net', 'Income before\nincome taxes', 268),
('Gross margin', 'Research and\ndevelopment', 5758), 
('Gross margin', 'Sales and marketing', 5379), 
('Gross margin', 'General and\nadministrative', 1384),
('Income before\nincome taxes', 'Net income', 18765), 
('Income before\nincome taxes', 'Provision for\nincome taxes', 3750),
]

s = Sankey(flows=flows, flow_color_mode='lesser')
s.draw()
plt.show()

sankey diagram

python sankey-diagram
1个回答
0
投票

关于字体大小,根据源代码,可以利用

SankeyNode
类:

该类定义并绘制桑基图中的单个节点。它 不需要创建 由最终用户创建,但可能希望在创建后编辑某些属性。

在您的代码中,如果您要将所有元素的字体大小设置为(比方说)28,并将所有标签右对齐,则在创建

s
变量后添加以下代码行即可而不是左边:

for node_list in s.nodes:
    for node in node_list:
        node.label_opts = {"fontsize": 28}
        node.label_pos='right'

要更改字体大小或单个特定节点的位置,您必须通过其索引或名称来识别它。例子:

#changing the third node at the fourth horizontal level (i.e. "Research and development")
s.nodes[3][2].label_pos='right'
s.nodes[3][2].label_opts = {"fontsize":18}

#changing the node called "Provision for income taxes"
node = s.find_node("Provision for\nincome taxes")[0]
node.label_pos='right'
node.label_opts = {"fontsize":25}

源代码中提供了用于自定义节点(颜色、填充等)的选项列表,请查看 __init__

 类的 
SankeyNode
 函数
的开头。

完整示例,结合上面的所有示例并将它们集成到您的代码中:

from sankeyflow import Sankey
import matplotlib.pyplot as plt
plt.figure(figsize=(20, 10), dpi=144)
flows = [
('Product', 'Total revenue', 20779),
('Sevice\nand other', 'Total revenue', 30949),
('Total revenue', 'Gross margin', 34768),
('Total revenue', 'Cost of revenue', 16960),
('Gross margin', 'Operating income', 22247),
('Operating income', 'Income before\nincome taxes', 22247, {'flow_color_mode': 'dest'}),
('Other income, net', 'Income before\nincome taxes', 268),
('Gross margin', 'Research and\ndevelopment', 5758), 
('Gross margin', 'Sales and marketing', 5379), 
('Gross margin', 'General and\nadministrative', 1384),
('Income before\nincome taxes', 'Net income', 18765), 
('Income before\nincome taxes', 'Provision for\nincome taxes', 3750),
]

s = Sankey(flows=flows, flow_color_mode='lesser')
#setting a font size of 28 for all node labels
for node_list in s.nodes:
    for node in node_list:
        node.label_opts["fontsize"] = 28
#changing the third node at the fourth horizontal level (i.e. "Research and development") - aligned to the right, font size of 18
s.nodes[3][2].label_pos='right'
s.nodes[3][2].label_opts = {"fontsize":18}
#changing the node called "Provision for income taxes", aligned to the right, font size of 25
node = s.find_node("Provision for\nincome taxes")[0]
node.label_pos='right'
node.label_opts = {"fontsize":25}
s.draw()
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.