Quantcast
Channel: 🎛️ Dash - Plotly Community Forum
Viewing all articles
Browse latest Browse all 6271

Issues with Assigning Colors to Dash Map Points

$
0
0

Hello,

This is my first time posting in here, so I apologize if I am not following the correct procedures. I am having some trouble assigning colors based off a categorical variable in my dataset on a map.

Here is my code:

import pandas as pd
import numpy as np

import plotly.graph_objects as go
import plotly.express as px

import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output


df = [[-77.070033, 38.955367, 'A'],
      [-74.070033, 38.955367, 'A'],
      [-77.070033, 38.755367, 'B'],
      [-77.070033, 38.655367, 'B'],
      [-77.070033, 38.555367, 'C'],
      [-77.070033, 38.455367, 'C'],
      [ -77.070033, 38.955367, 'A'],
      [ -77.070033, 38.955367, 'D'],
      [ -77.040033, 38.955367, 'D'],
      [ -77.090033, 38.155367, 'B'],
      [ -77.050033, 38.055367, 'C'],
      [ -77.070033, 38.655367, 'D'],
      [ -77.070033, 39.955367, 'A'],
      [ -77.070033, 40.955367, 'A'],
      [-76.070033, 38.955367, 'B'],
      [-74.070033, 38.955367, 'C']]

df=pd.DataFrame(df,columns=['X','Y','Group'])

groups = df['Group'].unique()

fig = go.Figure()

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig, 
              id='map'),
    dcc.Dropdown(
        id='group',
        options=[{'label':i,'value':i} for i in groups],
        value='A')
])

@app.callback(
    Output('map','figure'),
    Input('group','value'))

def update_figure(selected_group):
    filtered_df = df[df.Group == selected_group]
    
    
    fig = go.Figure(data=go.Scattergeo(
        locationmode = 'USA-states',
        lon = filtered_df['X'],
        lat = filtered_df['Y'],
        mode = 'markers',
        marker = dict(
            size = 8,
            opacity = 0.8,
            symbol = 'circle',
            line = dict(width=1))
        #,marker_color=groups
    ))

    fig.update_layout(
        title = 'Add title here',
        geo = dict(
            scope='usa',
            projection_type='albers usa',
            showland = True,
            landcolor = "rgb(250, 250, 250)",
            subunitcolor = "rgb(217, 217, 217)",
            countrycolor = "rgb(217, 217, 217)",
            countrywidth = 0.5,
            subunitwidth = 0.5
        )
    )



    return fig

app.run_server(host='0.0.0.0',port='8050') 

All I want to do is to color the points based on the group selection. If I include the color_marker option here, the map disappears. I’m not sure how to make this work properly. Can anyone help point me in the right direction?

Thank you!

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 6271

Trending Articles