使用 matplotlib.sankey 生成桑基图

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

我正在尝试使用下面的数据框绘制桑基图,其中源和目标以及流量或宽度是相应的数字。

这是我的代码示例

import pandas as pd
import numpy as np
from webcolors import hex_to_rgb
%matplotlib inline

from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True) 

import plotly.graph_objects as go # Import the graphical object


df = pd.read_excel('PathV1.xlsx', sheet_name=0, index_col=None, header=None)
df.head()

source = df[0].values.tolist()
target = df[0].values.tolist()
values = df[0].values.tolist()
node_label = df[0].values.tolist()
node_dict = {y:x for x, y in enumerate(node_label)}
source_node = [node_dict[x] for x in source]
target_node = [node_dict[x] for x in target]
fig = go.Figure( 
    data=[go.Sankey( # The plot we are interest
        # This part is for the node information
        node = dict( 
            label = node_label
        ),
        # This part is for the link information
        link = dict(
            source = source,
            target = target,
            value = values
        ))])
# With this save the plots 
plot(fig,
     image_filename='sankey_plot_1', 
     image='png', 
     image_width=1000, 
     image_height=600
)
# And shows the plot
fig.show()

这是我的数据框:enter image description here

The code appears to be working but i can't generate Sankey diagram.

我想有一个基于此数据框示例生成桑基图的想法。

python pandas plotly
© www.soinside.com 2019 - 2024. All rights reserved.