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
# Sample data for demonstration
states = ['California', 'Texas', 'Florida', 'New York', 'Illinois', 'Ohio', 'Pennsylvania']
percentages = [66, 42, 50, 60, 45, 48, 52] # Dummy vote percentage for a party
swing_states = ['Florida', 'Ohio'] # Example of swing states
# Create the electoral map
fig = go.Figure(go.Scattergeo())
# Update geos for the map
fig.update_geos(
visible=False,
resolution=110,
scope="usa",
showcountries=True,
countrycolor="Black",
showsubunits=True,
subunitcolor="Blue"
)
# Add states with color grading based on vote percentages
for state, percentage in zip(states, percentages):
color = f'rgb({255 * (100-percentage)/100}, {255 * percentage/100}, 0)'
fig.add_trace(go.Scattergeo(
locationmode='USA-states',
locations=[state],
text=f'{state}: {percentage}%',
marker=dict(size=15, color=color),
name=state,
showlegend=False
))
# Highlight swing states
for state in swing_states:
fig.add_trace(go.Scattergeo(
locationmode='USA-states',
locations=[state],
marker=dict(size=25, color='orange'),
name=state,
showlegend=True
))
# Update layout
fig.update_layout(height=600, margin={'r':0,'t':0,'l':0,'b':0}, title_text='Electoral Map with Vote Percentage')
# 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?