简体   繁体   中英

Plotly - python - How to loop through colors

I have a simple plot. How do I loop through colors in a list of rgb 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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