如何在 python 中的旭日图的所有层中为每个类别设置颜色?

How to setup a color per category accross all layers of a sunburst plotly graph in python?

我有一个如下所示的数据框 df:

step1    step2   step3   step4     occurances

Homepage Product Buy     Homepage 180
Homepage Product End     End      2000
Homepage End     End     End      150
Homepage Product Product Buy      100

我想创建一个旭日形图来可视化每个客户的路径。 到目前为止,这是我的代码:

fig =px.sunburst(
    df,
    path = ['step1', 'step2', 'step3', 'step4'],
    values = 'occurrances',
    color ='step2'
)
fig.show()

但是,我想定义每个图层的每个类别的颜色,而不仅仅是 'step2' 层,并在步骤 1、2、3 和 4 中为每个类别保持相同的颜色。 所以我想从这个开始 old graph to this new graph 有没有人知道如何做到这一点?

提前致谢

import io
import pandas as pd
import plotly.express as px

df = pd.read_csv(io.StringIO("""step1    step2   step3   step4     occurances
Homepage Product Buy     Homepage 180
Homepage Product End     End      2000
Homepage End     End     End      150
Homepage Product Product Buy      100"""), sep="\s+")

fig =px.sunburst(
    df,
    path = ['step1', 'step2', 'step3', 'step4'],
    values = 'occurances',
    color ='step2'
)
fig.update_traces(
    marker_colors=[
        px.colors.qualitative.D3[c] for c in pd.factorize(fig.data[0].labels)[0]
    ],
    leaf_opacity=1,
)