How do you make Plotly Express plot points in the colors specified in a DataFrame column?
I have a DataFrame with columns: names, latitudes, longitudes, and colors as below:
df = pd.DataFrame({'name': pd.Series((1, 2, 3, 4), dtype='int32'),
'LAT': pd.Series((40.62718, 40.63718, 40.74718, 40.75718), dtype='float64'),
'LON': pd.Series((-73.29494, -73.30494, -73.31494, -73.32494), dtype='float64'),
'color': ('#222A2A', '#2E91E5', '#FC0080', '#750D86')})
I cannot get Plotly Express to plot points in the color that I have designated in my DataFrame. It plots whatever color it wants, even with color_discrete_map='identity'
and color='colors'
, which is what the docs say to do.
color_discrete_map (dict with str keys and str values (default {})) β String values should define valid CSS-colors Used to override color_discrete_sequence to assign a specific colors to marks corresponding with specific values. Keys in color_discrete_map should be values in the column denoted by color. Alternatively, if the values of color are valid colors, the string βidentityβ may be passed to cause them to be used directly.
(plotly.express.scatter_mapbox β 5.18.0 documentation)
fig = px.scatter_mapbox(df, lat="LAT", lon="LON", hover_name='name',
hover_data=["name", "LAT", "LON", "color"],
color_discrete_map='identity', color='color', center=dict(lat=40.95004, lon=-73.07169),
zoom=7, height=300)
fig.update_layout(mapbox_style="open-street-map")
fig.update_layout(margin={"r":0,"t":0,"l":0,"b":0})
fig.update_layout(autosize=False, width=1500, height=500)
fig.show()
Please help.