gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:36:10 25 4
gpt4 key购买 nike

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com