有没有办法更改plotly.express.sunburst图形中树叶的不透明度?

2024-09-27 04:20:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我不喜欢叶子类别有不同的不透明度,我找不到改变它的设置

我找到了this related topic,但我使用的是plotly.express而不是plotly.graph\u对象。有没有办法改变它,或者我必须试着把我的px图形变成一个graph_对象? example image

import plotly.express as px
data = dict(
    character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    value=[10, 14, 12, 10, 2, 6, 6, 4, 4])

fig =px.sunburst(
    data,
    names='character',
    parents='parent',
    values='value',
)
fig.show()

Tags: 对象datavaluefigplotlyeve类别graph
2条回答

无论您是使用Plotly Express还是Plotly Graph Objects构建您的体形,这都是有效的:

fig.update_traces(leaf=dict(opacity = 1))

enter image description here

完整代码:

import plotly.express as px
data = dict(
    character=["Eve", "Cain", "Seth", "Enos", "Noam", "Abel", "Awan", "Enoch", "Azura"],
    parent=["", "Eve", "Eve", "Seth", "Seth", "Eve", "Eve", "Awan", "Eve" ],
    value=[10, 14, 12, 10, 2, 6, 6, 4, 4])

fig =px.sunburst(
    data,
    names='character',
    parents='parent',
    values='value',
)

fig.update_traces(leaf=dict(opacity = 1))

fig.show()

如相关主题中所述,透明度可以通过相应的图形对象进行控制

import plotly.graph_objects as go

fig =go.Figure(go.Sunburst(
    labels=data['character'],
    parents=data['parent'],
    values=[10, 14, 12, 10, 2, 6, 6, 4, 4],
    leaf=dict(opacity=1),
))

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

fig.show()

enter image description here

相关问题 更多 >

    热门问题