为了将color_discrete_map
用于级别2分段(状态),您需要设置color='Status'
color_discrete_map
中的键应该是由表示的列中的值
color
.
现在所有其他地段(非级别2,包括根)将具有相同的 colored颜色 ,这是我们不想要的.不过,我们可以使用 colored颜色 贴图中的特殊键"(?)"
覆盖该 colored颜色 (不会有太大帮助,因为它将影响所有剩余的地段,但我们仍然可以使用它来设置根 colored颜色 ,因为我们知道我们即将覆盖Level 1分段).
对于级别1(区域) colored颜色 ,我们可以从px.colors
中 Select 一个适当大的调色板(请参见Discrete Colors),然后手动分配(创建fig
后):
level2_colors = {
"(?)": "white", # assign "white" to all remaining sectors, including root
"Met": "blue",
"Compensates": "purple",
"Planned": "#AACC96",
"To Do": "orange",
}
df = pd.DataFrame(xdata, columns=['Area', 'Status', 'Value'])
fig = px.sunburst(df.assign(hole=" "),
path=['hole','Area', 'Status'],
values='Value',
color='Status',
color_discrete_map=level2_colors,
)
# Each sector id has its own color
colors = list(fig.data[0]['marker']['colors'])
ids = fig.data[0]['ids']
# print(list(zip(ids, colors)))
areas = df['Area'].unique()
palette = px.colors.qualitative.T10.copy()
# Reset color for each area
for i, id in enumerate(fig.data[0]['ids']):
if id.split('/')[-1] in areas:
colors[i] = palette.pop(0)
fig.data[0]['marker']['colors'] = colors
为了让所有标签都是径向的,只需添加:
fig.update_traces(insidetextorientation='radial')
在使用 colored颜色 映射表"(?)"
中的特殊键时,我发现了一个技巧,可以根据加载的template自动从默认调色板向其余地段分配不同的 colored颜色 (奇怪的是,设置要映射到这些地段的 colored颜色 数组并不起作用,它的行为与设置空数组或空字符串相同,无论如何它是通过从模板中挑选 colored颜色 来完成这项工作的):
level2_colors = {
# "(?)": "white", # assigns "white" to all remaining sectors
# "(?)": ["white", 'red', ...], # doesn't do what we'd expect (same as below)
"(?)": "", # assigns distinct colors from template
"Met": "blue",
"Compensates": "purple",
"Planned": "#AACC96",
"To Do": "orange",
}
df = pd.DataFrame(xdata, columns=['Area', 'Status', 'Value'])
fig = px.sunburst(df.assign(hole=" "),
path=['hole','Area', 'Status'],
values='Value',
color='Status',
color_discrete_map=level2_colors,
template='seaborn'
)