fig = go.Figure(go.Scattergeo()) fig.update_geos( visible=False, resolution=110, scope="usa", showcountries=True, countrycolor="Black", showsubunits=True, subunitcolor="Blue" ) fig.update_layout(height=300, margin={"r":0,"t":0,"l":0,"b":0}) fig.show() Use this to build an electoral Map, with a color grading for which state have % of vote for the party, use the red / blue gradient for the party, Highlight the swing state
To upload files, please first save the app
import streamlit as st
import plotly.graph_objects as go
import plotly.express as px
import pandas as pd
import us
from data import get_data
states = us.states
print(states)
percentages = [10, 15, 55, 81, 74, 61, 35] * (len(states) // 7 + 1) # Example data
data = get_data()
print(data)
data = {
'usa_state_code': [state["abbr"] for state in states],
'state': [state["name"] for state in states],
'percent_republican': percentages[:len(states)],
'percent_democrat': [100 - p for p in percentages[:len(states)]]
}
df = pd.DataFrame(data)
fig = px.choropleth(
df,
locations = 'usa_state_code',
locationmode = 'USA-states',
scope="usa",
color = 'percent_republican',
hover_name = 'state',
hover_data = ['percent_republican', 'percent_democrat'],
range_color = [10, 90],
color_continuous_scale = 'armyrose'
)
# Streamlit app structure
st.title('Electoral Vote Map')
st.plotly_chart(fig)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?