Python Plotly 中的节点标签未出现在 Sankey 中
Node labels not appearing in Sankey from Python Plotly
使用 Python Plotly 尝试我的第一个桑基流程图。 sankey 流程图完美地显示了正确的源和目标。但我无法包含节点标签。标签位于数据框内。没有各种源和目标的标签,该图是不完整的。这是我使用的代码。我的目的是得到一个静态图。
{link = dict(source = Service_df, target=Manufac_df, value=Revenue_df)}
{node = dict(label=Label_df, pad=50, thickness=5)}
{data = dict(type = 'sankey', hoverinfo = 'all', link = link, node=node)}
{fig = go.Figure(data)}
{fig.show() }
后来,我尝试硬编码源和目标的标签。那也没用。
当我将鼠标悬停在图表上时,我收到消息 Source:Undefined Target:Undefined。同时,将鼠标悬停在源或目标上时,可以看到传入和传出的流量计数。
我正在 Jupyter Notebook Python 3.8.5
中试用
数据帧如下:
{MainData.info()}
RangeIndex:31 个条目,0 到 30
数据列(共3列):
列非空计数 Dtype
0 服务 31 非空 int64
1 制造商 31 非空 int64
2 收入 31 非空 float64
dtypes: float64(1), int64(2)
{LabelData.info()}
RangeIndex:10 个条目,0 到 9
数据列(共1列):
列非空计数 Dtype
0 标签 10 非空对象
数据类型:对象(1)
我得到的图像在这里。 https://drive.google.com/drive/folders/1QDc-qVyMYSTJNI0coNJf8Ehuq8tlL-mP?usp=sharing
在没有看到您的数据帧的情况下,我假设您的 Label_df
与 link
中的数据帧不同 shape/size。
import pandas as pd
import plotly.graph_objects as go
# mock data, identical to the plotly documentation https://plotly.com/python/sankey-diagram/
Label_df = pd.DataFrame(["A1", "A2", "B1", "B2", "C1", "C2"])
Service_df = pd.DataFrame([0, 1, 0, 2, 3, 3])
Manufac_df = pd.DataFrame([2, 3, 3, 4, 4, 5])
Revenue_df = pd.DataFrame([8, 4, 2, 8, 4, 2])
link = dict(source = Service_df[0], target=Manufac_df[0], value=Revenue_df[0])
# this works
node = dict(label=Label_df[0], pad=50, thickness=5)
data = dict(type='sankey', hoverinfo='all', link=link, node=node)
fig = go.Figure(data)
fig.show()
# this gives Source:Undefined Target:Undefined
node = dict(label=list(Label_df[0]), pad=50, thickness=5)
data = dict(type='sankey', hoverinfo='all', link=link, node=node)
fig = go.Figure(data)
fig.show()
使用 Python Plotly 尝试我的第一个桑基流程图。 sankey 流程图完美地显示了正确的源和目标。但我无法包含节点标签。标签位于数据框内。没有各种源和目标的标签,该图是不完整的。这是我使用的代码。我的目的是得到一个静态图。
{link = dict(source = Service_df, target=Manufac_df, value=Revenue_df)}
{node = dict(label=Label_df, pad=50, thickness=5)}
{data = dict(type = 'sankey', hoverinfo = 'all', link = link, node=node)}
{fig = go.Figure(data)}
{fig.show() }
后来,我尝试硬编码源和目标的标签。那也没用。
当我将鼠标悬停在图表上时,我收到消息 Source:Undefined Target:Undefined。同时,将鼠标悬停在源或目标上时,可以看到传入和传出的流量计数。
我正在 Jupyter Notebook Python 3.8.5
中试用数据帧如下:
{MainData.info()}
列非空计数 Dtype
0 服务 31 非空 int64
1 制造商 31 非空 int64
2 收入 31 非空 float64
dtypes: float64(1), int64(2)
{LabelData.info()}
列非空计数 Dtype
0 标签 10 非空对象 数据类型:对象(1)
我得到的图像在这里。 https://drive.google.com/drive/folders/1QDc-qVyMYSTJNI0coNJf8Ehuq8tlL-mP?usp=sharing
在没有看到您的数据帧的情况下,我假设您的 Label_df
与 link
中的数据帧不同 shape/size。
import pandas as pd
import plotly.graph_objects as go
# mock data, identical to the plotly documentation https://plotly.com/python/sankey-diagram/
Label_df = pd.DataFrame(["A1", "A2", "B1", "B2", "C1", "C2"])
Service_df = pd.DataFrame([0, 1, 0, 2, 3, 3])
Manufac_df = pd.DataFrame([2, 3, 3, 4, 4, 5])
Revenue_df = pd.DataFrame([8, 4, 2, 8, 4, 2])
link = dict(source = Service_df[0], target=Manufac_df[0], value=Revenue_df[0])
# this works
node = dict(label=Label_df[0], pad=50, thickness=5)
data = dict(type='sankey', hoverinfo='all', link=link, node=node)
fig = go.Figure(data)
fig.show()
# this gives Source:Undefined Target:Undefined
node = dict(label=list(Label_df[0]), pad=50, thickness=5)
data = dict(type='sankey', hoverinfo='all', link=link, node=node)
fig = go.Figure(data)
fig.show()