Spaces:
Runtime error
Runtime error
import streamlit as st | |
import pandas as pd | |
import plotly.graph_objects as go | |
import plotly.express as px | |
# Define the states and conditions of interest | |
states = ["Minnesota", "Florida", "California"] | |
top_n = 10 | |
# Define the list dictionary of top 10 health conditions descending by cost, with emojis | |
health_conditions = [ | |
{"condition": "๐ Heart disease", "emoji": "๐", "spending": 214.3}, | |
{"condition": "๐ค Trauma-related disorders", "emoji": "๐", "spending": 198.6}, | |
{"condition": "๐ฆ Cancer", "emoji": "๐๏ธ", "spending": 171.0}, | |
{"condition": "๐ง Mental disorders", "emoji": "๐ง", "spending": 150.8}, | |
{"condition": "๐ฆด Osteoarthritis and joint disorders", "emoji": "๐ฅ", "spending": 142.4}, | |
{"condition": "๐ Diabetes", "emoji": "๐ฉธ", "spending": 107.4}, | |
{"condition": "๐ซ Chronic obstructive pulmonary disease and asthma", "emoji": "๐ซ", "spending": 91.0}, | |
{"condition": "๐ฉบ Hypertension", "emoji": "๐", "spending": 83.9}, | |
{"condition": "๐ฌ Hyperlipidemia", "emoji": "๐ฌ", "spending": 83.9}, | |
{"condition": "๐ฆด Back problems", "emoji": "๐ง", "spending": 67.0} | |
] | |
# Total the spending values | |
total_spending = sum([hc["spending"] for hc in health_conditions]) | |
# Create a DataFrame from the list dictionary | |
df_top_conditions = pd.DataFrame(health_conditions) | |
# Create the map graph using Plotly Express | |
fig_map = px.choropleth(locations=["CA", "FL", "MN"], locationmode="USA-states", color=[1, 2, 3], scope="usa") | |
fig_map.update_layout(geo=dict(bgcolor= "rgba(0, 0, 0, 0)", lakecolor="rgb(255, 255, 255)")) | |
# Create the bar chart using Plotly Graph Objects | |
fig_bar = go.Figure(go.Bar(x=df_top_conditions["emoji"] + " " + df_top_conditions["condition"], y=df_top_conditions["spending"], marker_color="#1f77b4")) | |
fig_bar.update_layout(title=f"Top {top_n} Health Conditions in {', '.join(states)} by Spending (Total: ${total_spending}B)", | |
yaxis_title="Spending ($B)", plot_bgcolor='rgba(0,0,0,0)', xaxis=dict(showgrid=False), yaxis=dict(showgrid=False)) | |
# Display the map and the bar chart in Streamlit | |
col1, col2 = st.columns([2, 1]) | |
with col1: | |
st.plotly_chart(fig_map, use_container_width=True) | |
with col2: | |
st.plotly_chart(fig_bar, use_container_width=True) | |