生成旭日图时页面为空

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

我试图编写一个函数来生成旭日图,但该图显示了一个空白页。没有错误消息。然后我用简单的示例数据进行测试(见下文),但它仍然是空白。当我使用plotly.express 而不是plotly.graph_objects 时,它就可以工作。有人知道为什么吗?感谢您的帮助!

import pandas as pd
import plotly.graph_objects as go
# Sample data
data = {
    'Category': ['A', 'A', 'B', 'B'],
    'Subcategory': ['A1', 'A2', 'B1', 'B2'],
    'Value': [10, 20, 30, 40]
}
df = pd.DataFrame(data)

# Create a basic sunburst chart
fig = go.Figure(go.Sunburst(
    labels=df['Subcategory'],
    parents=df['Category'],
    values=df['Value']
))

fig.update_layout(margin=dict(t=0, l=0, r=0, b=0))

# Save the chart to an HTML file
fig.write_html('basic_hierarchical_sunburst_chart.html')
python plotly sunburst-diagram plotly.graph-objects
1个回答
0
投票

您的示例数据不是有效的旭日图。

labels
应该是所有唯一元素的列表,并且
parents
必须包含
labels
中的条目,或者如果元素没有父元素,则为空字符串。您定义元素
['A1', 'A2', 'B1', 'B2']
,然后将 A 和 B(不存在)指定为父元素。

将示例数据更改为

data = {
    'Category': ['','A', 'A','', 'B', 'B'],
    'Subcategory': ['A','A1', 'A2','B', 'B1', 'B2'],
    'Value': [10, 20, 30, 40, 10, 20]
}

我得到了图表

© www.soinside.com 2019 - 2024. All rights reserved.