gpt4 book ai didi

python - Plotly:如何从数据框中绘制桑基图?

转载 作者:行者123 更新时间:2023-12-04 12:13:35 25 4
gpt4 key购买 nike

我有一个数据框:

Vendor Name                 Category                    Count
AKJ Education Books 846888
AKJ Education Computers & Tablets 1045
Amazon Books 1294423
Amazon Computers & Tablets 42165
Amazon Other 415
Flipkart Books 1023

我正在尝试使用上述数据框绘制桑基图,来源为 供应商名称 目标是 分类 ,并且流量或宽度为 计数 .我尝试使用 Plotly,但没有成功。有没有人有使用 Plotly 制作桑基图的解决方案?

谢谢

最佳答案

帖子回复How to define the structure of a sankey diagram using a dataframe?将向您展示强制您的 Sankey 数据源进入一个数据帧可能会很快导致困惑。最好将节点与链接分开,因为它们的构造不同。

所以你的节点数据框应该是这样的:

ID               Label    Color
0 AKJ Education #4994CE
1 Amazon #8A5988
2 Flipkart #449E9E
3 Books #7FC241
4 Computers & tablets #D3D3D3
5 Other #4994CE

您的链接数据框应如下所示:
Source  Target      Value      Link Color
0 3 846888 rgba(127, 194, 65, 0.2)
0 4 1045 rgba(127, 194, 65, 0.2)
1 3 1294423 rgba(211, 211, 211, 0.5)
1 4 42165 rgba(211, 211, 211, 0.5)
1 5 415 rgba(211, 211, 211, 0.5)
2 5 1 rgba(253, 227, 212, 1)

现在,如果您使用与 plot.ly 上的苏格兰公投图类似的设置,你将能够构建这个:

enter image description here

由于数字之间的巨大差异,该特定图表看起来有点奇怪。出于说明目的,我已将您所有的数字替换为 1 :

enter image description here

这是一个简单的复制和粘贴到 Jupyter Notebook 的全部内容:
# imports
import pandas as pd
import numpy as np
import plotly.graph_objs as go
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

# Nodes & links
nodes = [['ID', 'Label', 'Color'],
[0,'AKJ Education','#4994CE'],
[1,'Amazon','#8A5988'],
[2,'Flipkart','#449E9E'],
[3,'Books','#7FC241'],
[4,'Computers & tablets','#D3D3D3'],
[5,'Other','#4994CE'],]

# links with your data
links = [['Source','Target','Value','Link Color'],

# AKJ
[0,3,1,'rgba(127, 194, 65, 0.2)'],
[0,4,1,'rgba(127, 194, 65, 0.2)'],

# Amazon
[1,3,1,'rgba(211, 211, 211, 0.5)'],
[1,4,1,'rgba(211, 211, 211, 0.5)'],
[1,5,1,'rgba(211, 211, 211, 0.5)'],

# Flipkart
[2,5,1,'rgba(253, 227, 212, 1)'],
[2,3,1,'rgba(253, 227, 212, 1)'],]

# links with some data for illustrative purposes ################
#links = [
# ['Source','Target','Value','Link Color'],
#
# # AKJ
# [0,3,846888,'rgba(127, 194, 65, 0.2)'],
# [0,4,1045,'rgba(127, 194, 65, 0.2)'],
#
# # Amazon
# [1,3,1294423,'rgba(211, 211, 211, 0.5)'],
# [1,4,42165,'rgba(211, 211, 211, 0.5)'],
# [1,5,415,'rgba(211, 211, 211, 0.5)'],
#
# # Flipkart
# [2,5,1,'rgba(253, 227, 212, 1)'],]
#################################################################


# Retrieve headers and build dataframes
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)

# Sankey plot setup
data_trace = dict(
type='sankey',
domain = dict(
x = [0,1],
y = [0,1]
),
orientation = "h",
valueformat = ".0f",
node = dict(
pad = 10,
# thickness = 30,
line = dict(
color = "black",
width = 0
),
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'),
)
)

layout = dict(
title = "Draw Sankey Diagram from dataframes",
height = 772,
font = dict(
size = 10),)

fig = dict(data=[data_trace], layout=layout)
iplot(fig, validate=False)

关于python - Plotly:如何从数据框中绘制桑基图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com