Build an electoral map of the US, this one need to display each state, with an interactive map where people can click on understand if in details. Create a effect for the transition of the US map, and State map. Provide mock data of now , focus on the map.
To upload files, please first save the app
import streamlit as st
import plotly.express as px
import pandas as pd
# Mock data for 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'],
'Votes': [9, 3, 11, 6, 55, 9, 7, 3, 29, 16,
4, 4, 20, 11, 6, 8, 8, 8, 4, 10,
11, 15, 10, 6, 10, 3, 5, 6,
4, 14, 5, 29, 15, 3, 18, 7,
7, 20, 4, 11, 8, 38, 6, 3,
3, 13, 10, 3, 10, 1]
}
# Create DataFrame
states_df = pd.DataFrame(states_data)
# Create a choropleth map
fig = px.choropleth(states_df,
geojson="https://raw.githubusercontent.com/PublicaMundi/MappingAPI/master/data/geojson/us-states.geojson",
locations='State',
featureidkey="properties.name",
color="Votes",
color_continuous_scale="Viridis",
scope="usa",
labels={'Votes': 'Electoral Votes'},
title='US Electoral Votes by State')
# Update layout for better interaction
fig.update_geos(fitbounds="locations", visible=False)
fig.update_layout(transition_duration=500)
# Display the plot
st.plotly_chart(fig, use_container_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?