Skip to content

How to modify points drawn on map using a dropdown menu #3711

@ghost

Description

Here's how I create the map and add a dropdown menu with option1 and option2.

import pandas as pd
import plotly.express as px

us_cities = pd.read_csv(
    'https://raw.githubusercontent.com/plotly/datasets/master/us-cities-top-1k.csv'
)


fig = px.scatter_mapbox(
    us_cities,
    lat='lat',
    lon='lon',
    hover_name='City',
    hover_data=['State', 'Population'],
    color_discrete_sequence=['fuchsia'],
    zoom=3,
)
fig.update_layout(mapbox_style='open-street-map')
fig.update_layout(margin={'r': 0, 't': 0, 'l': 0, 'b': 0})
fig.update_layout(
    updatemenus=[
        dict(
            buttons=list(
                [
                    {
                        'args': ['type', 'option1'],
                        'label': 'Option 1',
                        'method': 'restyle',
                    },
                    {
                        'args': ['type', 'option2'],
                        'label': 'Option 2',
                        'method': 'restyle',
                    },
                ]
            ),
            direction='down',
            x=0.075,
            xanchor='right',
            yanchor='bottom',
        ),
    ]
)
fig.show()

I want to have all states listed in the dropdown menu, with an option to plot all data points of all states when set to all or option1 or whatever the name is, or alternatively, to select a state and only the data points belonging to the state to be shown and the rest are deleted.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions