python - 如何更改plot.ly 中旭日图的颜色?

标签 python plotly

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

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()

最佳答案

我认为如果您可以根据数据制作 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()

enter image description here

关于python - 如何更改plot.ly 中旭日图的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62578171/

相关文章:

python - 尝试在python中打印模块内的每个子模块

python - CKAN 最大上传大小

r - 通过因子 plotly R 绘制和跟踪相同的颜色

javascript - 我可以使用 Plotly 制作动画图吗?

r - 如何将图例从颜色条更改为 "regular legend",仍然使用颜色渐变

python - 如何避免具有多列的 numpy 数组的总和精度较低

python - 使用 Jenkins 进行测试时处理硬件资源

python-3.x - 查询多个数据框并绘制组直方图

r - plotly 错误 "WebGL is not supported by your browser"

python - 如何链接多个全息图中的轴?