首页 > 解决方案 > 如何更改 plot.ly 中旭日形图的颜色?

问题描述

我希望所有的哺乳动物都是黄色的,所有的爬行动物都是绿色的。我不能使用情节快递,因为我的学校不允许。代码如下:

import plotly.graph_objs as go

fig = go.Figure(go.Sunburst(
    labels=["Animal", "Reptile", "Lizard", "Snake", "Bird", "Salamander", 
            "Canary", "tweetle", "Mammal", "Equine", "Bovine", "Canine", 
            "Horse", "Zebra", "Cow", "Lassle", "Rintintin", "Bessle"],
    parents=["", "Animal", "Reptile", "Reptile", "Reptile", "Lizard",
            "Bird", "Canary", "Animal", "Mammal", "Mammal", "Mammal",
            "Equine", "Equine", "Bovine", "Canine", "Canine", "Cow"],
))


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

fig.show()

标签: pythonplotly

解决方案


df我认为如果你可以利用你的数据做一些逆向工程,我认为有更好的方法,px你可以尝试

import plotly.graph_objs as go

labels = ["Animal", "Reptile", "Lizard", "Snake", "Bird", "Salamander", 
          "Canary", "tweetle", "Mammal", "Equine", "Bovine", "Canine", 
          "Horse", "Zebra", "Cow", "Lassle", "Rintintin", "Bessle"]

parents = ["", "Animal", "Reptile", "Reptile", "Reptile", "Lizard",
           "Bird", "Canary", "Animal", "Mammal", "Mammal", "Mammal",
           "Equine", "Equine", "Bovine", "Canine", "Canine", "Cow"]

colors = []
for p in labels:
    if p in ["Reptile", "Lizard", "Snake", "Bird", "Salamander",
             "Canary", "tweetle"]:
        colors.append("green")
    elif p in ["", "Animal"]:
        colors.append("white")
    else:
        colors.append("yellow")

fig = go.Figure(
    go.Sunburst(
     labels=labels,
     parents=parents,
     marker=dict(colors=colors)
    )
)


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

fig.show()

在此处输入图像描述


推荐阅读