我正在按照此处的教程使用plotly 制作旭日图。我的数据是颜色词(\'name\')、颜色十六进制代码(\'hex\')、它们出现的次数(\'n\')以及父颜色或颜色类别(\'parent \'),以及关联的十六进制代码(\'parentHex\')。它看起来像这样:
\n\n name hex n parent parentHex\n-----------------------------------------\n0 dust #b2996e 3 orange #FFA500\n1 tea #65ab7c 16 grey #BEBEBE\n2 spruce #0a5f38 1 black #000000\n3 desert #ccad60 6 orange #FFA500\n4 swamp #698339 2 grey #BEBEBE\n5 fern #63a950 1 green #00FF00\n6 straw #fcf679 9 yellow #FFFF00\n7 leather #ac7434 5 orange #FFA500\n8 hazel #8e7618 2 orange #FFA500\n9 ice #d6fffa 4 white #FFFFFF\n\nRun Code Online (Sandbox Code Playgroud)\n\n我的代码如下所示:
\n\n name hex n parent parentHex\n-----------------------------------------\n0 dust #b2996e 3 orange #FFA500\n1 tea #65ab7c 16 grey #BEBEBE\n2 spruce #0a5f38 1 black #000000\n3 desert #ccad60 6 orange #FFA500\n4 swamp #698339 2 grey #BEBEBE\n5 fern #63a950 1 green #00FF00\n6 straw #fcf679 9 yellow #FFFF00\n7 leather #ac7434 5 orange #FFA500\n8 hazel #8e7618 2 orange #FFA500\n9 ice #d6fffa 4 white #FFFFFF\n\nRun Code Online (Sandbox Code Playgroud)\n\n这会生成一张像我想要的那样的旭日图,但颜色都是错误的。如何设置颜色以使值“orange”实际上是橙色,等等?
\n\n我也尝试过这个:
\n\nimport plotly.express as px\n\nfig = px.sunburst(dfSubset, path=[\'parent\', \'name\'], values=\'n\', color=\'hex\') \nfig.show()\nRun Code Online (Sandbox Code Playgroud)\n\n但这不起作用,要么\xe2\x80\x94我得到不同的颜色,但它们不是正确的颜色。
\n当我将“父项”重命名为“父项”(或父项以外的任何内容)时,旭日图的颜色将按预期返回。也许“父母”是一个保留词。
# Rename "parent" column to "parents"
dfSubset.rename(columns={'parent': 'parents'}, inplace=True)
# Create colour map
colorMapSubset = dict(zip(dfSubset.parents, dfSubset.parents))
fig = px.sunburst(dfSubset,
path=['parents', 'name'],
values='n',
color='parents',
color_discrete_map=colorMapSubset)
fig.show()
Run Code Online (Sandbox Code Playgroud)
(我使用的是 Plotly 版本“4.10.0”。)