I am trying to use plotly scattergeo to plot points in different categories specific colors using the update_trace method, but when I do it just renders all of the points black.
colors colors=('#1e90ff','#ff8c00','#2e8b57')
fig = go.Figure(data=go.Scattergeo(
lon = combined_frame['lon'],
lat = combined_frame['lat'],
text = combined_frame['tenant_name'] + '<br>' + combined_frame['facility_name'],
mode = 'markers',
#marker=(dict(color=list(colors))))
marker_color = (np.array(combined_frame.naics_two).astype(int)
)))
#fig.update_traces(marker=dict(color=list(colors)))
fig.update_layout(
title = 'Facility Locations<br>(Hover for tenant and facility names)',
geo_scope='usa',
#showlegend=True
)
fig.show()
There is a setting for marker color, but its value is unknown, so I am not sure what the cause is. The current code should be color coded with the value specified for the color in the default color map. For example, if the range of that value is extremely narrow, they will all be the same color.
To color-code the markers with the specified color, you would have to extract and set the color in the column of the data frame to be color-coded.
Using the U.S. airport arrival/departure frequency data in the reference, the frequencies are divided into three categories and color-coded by those categories. The reference can be found here.
import plotly.graph_objects as go
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/2011_february_us_airport_traffic.csv')
df['text'] = df['airport'] + '' + df['city'] + ', ' + df['state'] + '' + 'Arrivals: ' + df['cnt'].astype(str)
# Classify frequency
df['scale'] = pd.cut(df['cnt'], 3, labels=['Small', 'Medium', 'Large'])
colors = ['#1e90ff','#ff8c00','#2e8b57']
fig = go.Figure()
for s,c in zip(df['scale'].unique(), colors):
# print(s,c)
dff = df.query('scale == @s')
fig.add_trace(go.Scattergeo(
lon = dff['long'],
lat = dff['lat'],
text = dff['text'],
mode = 'markers',
name = s,
marker = dict(
size=dff['cnt']/20,
color = c,
line_color='rgb(40,40,40)',
line_width=0.5,
sizemode='area',
)
))
fig.update_layout(
title = 'Most trafficked US airports<br>Classify frequency',
geo_scope='usa',
)
fig.update_layout(autosize=True, height=600)
fig.show()