How to set color legend manually for python plotly open street map

Viewed 832

is there any way to set color legend manually for python plotly open street map. For example: instead of the legend color range , need to set the first 5 values: Red and next 5 blue and then yellow. enter image description here

Code:

import plotly.express as px
px.set_mapbox_access_token(open(".mapbox_token").read())
df = px.data.carshare()
fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon",     color="peak_hour", size="car_hours",
                  color_continuous_scale=px.colors.cyclical.IceFire, size_max=15, zoom=10)
fig.show()

The legend should be like this: enter image description here

2 Answers

You need to change color_continuous_scale parameter in px.scatter_mapbox() like this:

color_scale = [(0, 'blue'), (.25, 'blue'), (.25, 'green'), (.5, 'green'),\
           (0.5, 'red'), (0.75, 'red'), (0.75, 'yellow'), (1, 'yellow')]
color_continuous_scale = color_scale 

For more details, you can check out this link

access_token = 'Your Token'
px.set_mapbox_access_token(access_token)
import plotly.express as px
#px.set_mapbox_access_token(open(".mapbox_token").read())
df = px.data.carshare()
fig = px.scatter_mapbox(df, lat="centroid_lat", lon="centroid_lon",     color="peak_hour", size="car_hours",
color_continuous_scale=[(0.00, "red"),   (0.33, "red"),
                                                     (0.33, "green"), (0.66, "green"),
                                                     (0.66, "blue"),  (1.00, "blue")]                  
, size_max=15, zoom=10)
fig.show()
Related