生成旭日图时出现空白页面
我在尝试写一个函数来生成太阳辐射图,但结果却显示了一张空白的页面。没有任何错误信息。然后我用一些简单的示例数据进行测试(见下文),但结果还是空白。当我使用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')
# using plotly.express then it works
import pandas as pd
import plotly.express as px
# Sample data
data = {
'Category': ['A', 'A', 'B', 'B'],
'Subcategory': ['A1', 'A2', 'B1', 'B2'],
'Value': [10, 20, 30, 40]
}
df = pd.DataFrame(data)
# Create a sunburst chart using Plotly Express
fig = px.sunburst(df, path=['Category', 'Subcategory'], values='Value')
# Show the chart
fig.show()
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]
}
我就能得到这个图表
我不太明白你说的“当我使用 plotly.express 而不是 plotly.graph_objects 时,它就能工作。”你能提供完整的代码吗?用你的数据,plotly.express
对我来说也会显示一个空白页面。