我希望所有哺乳动物都是黄色,所有爬行动物都是绿色。我不能使用 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()
关于python - 如何更改plot.ly 中旭日图的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62578171/