简体   繁体   中英

Change marker colors based on values - Plotly

I want to change the marker colors based on values , so i tried the below piece of code

import pandas as pd
df = pd.read_csv("E:/values.csv")
def SetColor(x):
    if(x < 100):
        return "orange"
    elif(100<= x <=200):
        return "white"
    elif(x > 200):
        return "black"


import plotly.offline as pyo
import plotly.graph_objs as go
trace1 = go.Scatter(
                    x=df['Date'], y=df['Show1'], 
                    line = dict(color=list(map(SetColor, df['Show1']))),
                    mode='markers',name='Show1' 
                   )

fig = go.Figure(data=[trace1], layout=layout)
pyo.plot(fig,filename='final_plot.html')

But i am getting below error msge

ValueError: 
    Invalid value of type 'builtins.list' received for the 'color' property of scatter.line
        Received value: ['black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'white', 'white', 'white', 'white', 'white', 'white', 'white', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'black', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange', 'orange']

What am i missing here

The reason your current code isn't working is because you're setting line colors which doesn't accept lists, instead of marker colors, which does accept lists.

Instead of

line = dict(color=list(map(SetColor, df['Show1'])))

you can use

marker = dict(color=list(map(SetColor, df['Show1'])))

to set the marker colors.

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