I have a dataframe with lat, lon, and value. If the value is 0 I want blue, if it is >20 red and if <20 green.
Is it possible to use something like this function? If yes, how can I include this in the color field of Plotly so it takes the df[‘value’] as an input?
That function will work in a modified sense. I would just pass the entire data and create a list of blue, red, green. You can then just pass that list to a color argument in the code of a go.Scattermapbox. See below:
fig = go.Figure()
fig.add_trace(go.Scattermapbox(
lat=df['lat'].tolist(),
lon=df['lng'].tolist(),
mode='markers',
marker=go.scattermapbox.Marker(
size=4,
color='#E31837' #list or singular color here
),
#text = df['personal_lastName'].tolist(),
hoverinfo='none',
)
)
This is what your function should look like. You’ll pass in your data frame, and get a list of the column you want to color base on
def SetColor(df):
values = df[column].tolist()
color_list = []
for i in values:
if(i == 0):
color_list.append("blue")
elif(i > 20):
color_list.append("red")
elif(i<20):
color_list.append("green")
return color_list
Then you’ll call that function to return your list of colors.
fig = go.Figure()
fig.add_trace(go.Scattermapbox(
lat=df['lat'].tolist(),
lon=df['lng'].tolist(),
mode='markers',
marker=go.scattermapbox.Marker(
size=4,
color= SetColor(df) #function gets called here and will return a list of colors, (i.e. ['green', 'blue', 'red', 'green'])
),
)
)