Python Plotly:如何使用 Pandas 数据框定义 Sankey 图的结构?

桑基图用于通过定义“源”来表示源节点和“目标”来表示目标节点来可视化流程。它用于表示对象在不同数据点之间的流动。

在本教程中,让我们了解如何使用数据框定义桑基图的结构。在这里,我们将使用plotly.graph_objects模块来生成图形。它包含很多生成图表的方法。

步骤1

导入plotly.graphs_objs模块和别名go

importplotly.graphs_objsas go

第2步

将 Pandas 模块和别名导入为pd。

import pandas as pd

步骤 3

创建具有“id”、“label”和“color”坐标的二维节点列表 -

nodes = [
   ['id', 'label', 'color'],
   [ 0,    'A1',    'blue'],
   [ 1,    'A2',    'green'],
   [ 2,    'B1',    'red'],
   [ 3,    'B2',    'brown'],
   [ 4,    'C1',    'cyan'],
   [ 5,    'C2',    'yellow']
]

步骤4

为“源”、“目标”、“值”和“链接颜色”创建一个二维链接列表,定义如下 -

links = [
   ['Source', 'Target', 'Value', 'Link Color'],
   [  0,          2,       4,       'grey'],
   [  0,          3,       4,       'grey'],
   [  1,          3,       4,       'grey'],
   [  2,          4,       4,       'grey'],
   [  3,          4,       4,       'grey'],
   [  3,          5,       4,       'grey']
]

步骤 5

计算节点和链接头并为节点和链接生成两个数据帧。

nodes_headers = nodes.pop(0)
links_headers = links.pop(0)

df_nodes = pd.DataFrame(nodes, columns = nodes_headers)
df_links = pd.DataFrame(links, columns = links_headers)

步骤 6

接下来,创建桑基图。将数据框节点添加到 Sankey 图并设置其颜色和厚度值。

fig = go.Figure(data=[go.Sankey(
   node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = df_nodes['label'].dropna(axis=0, how='any'),
      color = df_nodes['color']),

步骤 7

生成“源”、“目标”、“值”和“链接颜色”的链接,如下所示 -

link = dict(
   source = df_links['Source'].dropna(axis=0, how='any'),
   target = df_links['Target'].dropna(axis=0, how='any'),
   value = df_links['Value'].dropna(axis=0, how='any'),
   color = df_links['Link Color'].dropna(axis=0, how='any'),
)

步骤 8

使用update_layout()方法设置桑基图的标题。最后,使用 显示图表。fig.show()

fig.update_layout(
   title_text="DataFrame-Sankey diagram",
   font_size=10
)
fig.show()

示范

使用 Pandas 数据框定义 Sankey 图结构的完整代码如下 -

importplotly.graph_objectsas go
import pandas as pd
nodes = [
   ['id', 'label', 'color'],
   [0, 'A1', 'blue'],
   [1, 'A2', 'green'],
   [2, 'B1', 'red'],
   [3, 'B2', 'brown'],
   [4, 'C1', 'cyan'],
   [5, 'C2', 'yellow']
]
links = [
   ['Source', 'Target', 'Value', 'Link Color'],
   [0, 2, 4, 'grey'],
   [0, 3, 4, 'grey'],
   [1, 3, 4, 'grey'],
   [2, 4, 4, 'grey'],
   [3, 4, 4, 'grey'],
   [3, 5, 4, 'grey']
]

#检索标头并构建数据框
nodes_headers = nodes.pop(0)
links_headers = links.pop(0)

df_nodes = pd.DataFrame(nodes, columns=nodes_headers)
df_links = pd.DataFrame(links, columns=links_headers)

fig = go.Figure(data=[go.Sankey(
   node = dict(
      pad = 15,
      thickness = 20,
      line = dict(color = "black", width = 0.5),
      label = df_nodes['label'].dropna(axis=0, how='any'),
      color = df_nodes['color']
   ),

   link = dict(
      source=df_links['Source'].dropna(axis=0, how='any'),
      target=df_links['Target'].dropna(axis=0, how='any'),
      value=df_links['Value'].dropna(axis=0, how='any'),
      color=df_links['Link Color'].dropna(axis=0, how='any'),
   )
)])

fig.update_layout(
   title_text="DataFrame-Sankey diagram",
   font_size=10
)

fig.show()
输出结果

执行时,它将在浏览器上显示以下输出 -


猜你喜欢