make an app that lets a user select us stTES AND SEE IT VISUALZED ON MAP
To upload files, please first save the app
import streamlit as st
import pandas as pd
import numpy as np
st.title("US States Map Visualization")
# Load US states data with coordinates (approximately center of each state)
states_data = {
'state': [
'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho',
'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada',
'New Hampshire', 'New Jersey', 'New Mexico', 'New York',
'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon',
'Pennsylvania', 'Rhode Island', 'South Carolina', 'South Dakota',
'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington',
'West Virginia', 'Wisconsin', 'Wyoming'
],
'latitude': [
32.7794, 64.0685, 34.2744, 34.8938, 36.7783, 39.5501,
41.6032, 39.0458, 27.6648, 32.1656, 19.8968, 44.2394,
40.0417, 39.8647, 41.9383, 38.5266, 37.8393, 30.9843,
45.2538, 39.0458, 42.4072, 44.3148, 46.7296, 32.3547,
38.5767, 46.8797, 41.4925, 38.8026, 43.1939, 40.0583,
34.5199, 42.1497, 35.7596, 47.5515, 40.4173, 35.0078,
44.5720, 40.2731, 41.5801, 33.8361, 44.3683, 35.5175,
31.9686, 40.7608, 44.5588, 37.4316, 47.7511, 38.5976,
44.2563, 42.7475
],
'longitude': [
-86.8287, -152.2782, -111.6602, -92.4426, -119.4179, -105.7821,
-72.7559, -75.4192, -81.5158, -82.9001, -155.5828, -114.5103,
-89.4937, -86.2604, -93.3898, -96.7265, -84.2700, -91.9623,
-69.4455, -76.6413, -71.3824, -85.6024, -94.6859, -89.3985,
-92.1735, -110.3626, -99.9018, -116.4194, -71.5724, -74.4057,
-105.8701, -74.1454, -79.0193, -101.0020, -82.9071, -97.0929,
-122.0709, -77.9055, -71.4774, -81.1637, -100.3510, -86.5804,
-99.9018, -111.8910, -72.5778, -78.6569, -120.7401, -80.4549,
-89.6385, -107.2085
]
}
df = pd.DataFrame(states_data)
# Create multiselect widget for states
selected_states = st.multiselect(
"Select states to visualize",
options=df['state'].tolist(),
default=['California', 'New York', 'Texas']
)
# Filter dataframe based on selection
if selected_states:
filtered_df = df[df['state'].isin(selected_states)]
# Display map
st.map(
filtered_df,
latitude='latitude',
longitude='longitude',
size=50000, # Size of the points
color='#FF0000' # Red color for visibility
)
# Display selected states in a table
st.subheader("Selected States")
st.table(filtered_df[['state', 'latitude', 'longitude']])
else:
st.warning("Please select at least one state to visualize")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?