简体   繁体   English

Plotly - python - 如何循环通过 colors

[英]Plotly - python - How to loop through colors

I have a simple plot.我有一个简单的 plot。 How do I loop through colors in a list of rgb colors?如何在 rgb colors 列表中循环遍历 colors?

import plotly.express as px

X1 = [1,2,3]
Y1 = [2,4,8]
Z1 = [3,6,8]

colors = ['rgb(255,0,0)','rgb(255,165,0)','rgb(0,0,255)']


cat = ['A','B','C']


    
figtest = px.scatter_3d(x = X1, y = Y1, z = Z1, color = cat)
figtest.update_traces(marker_color = [x for x in colors])
figtest.show()

What I got:我得到了什么: 在此处输入图像描述

I didn't have to loop, just pass the list我不必循环,只需传递列表

X1 = [1,2,3,4]
Y1 = [2,4,8,9]
Z1 = [3,6,8,10]

colors = ['rgb(255,0,0)','rgb(255,165,0)','rgb(0,0,255)','rgb(0,1,133)']


cat = ['A','B','C','D']

figtest = px.scatter_3d(x = X1, y = Y1, z = Z1, color = cat, color_discrete_sequence = colors )

figtest.show()

作品

you can create a dictionary of categories and then pass it through the color_discrete_map parameter like so - it'll then look up the color based on the cat value:您可以创建一个类别字典,然后像这样通过 color_discrete_map 参数传递它 - 然后它将根据 cat 值查找颜色:

import plotly.express as px

X1 = [1, 2, 3]
Y1 = [2, 4, 8]
Z1 = [3, 6, 8]
cat = ['A', 'B', 'C']

color_map = {'A': 'rgb(255,0,0)', 'B': 'rgb(255,165,0)', 'C': 'rgb(0,0,255)'}

figtest = px.scatter_3d(x=X1, y=Y1, z=Z1, color=cat, color_discrete_map=color_map)
figtest.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM