-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode 4.py
More file actions
95 lines (79 loc) · 3.61 KB
/
Copy pathcode 4.py
File metadata and controls
95 lines (79 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import pandas as pd
import plotly.express as px
from plotly.subplots import make_subplots
# --- Part 1: Prepare Data for Each of the 3 Charts ---
# --- Chart 1 Data (Monthly Missed Appointments Trend) ---
monthly_data = {
'Month': ['Jan 2024', 'Feb 2024', 'Mar 2024', 'Apr 2024', 'May 2024', 'Jun 2024', 'Jul 2024', 'Aug 2024', 'Sep 2024', 'Oct 2024', 'Nov 2024', 'Dec 2024', 'Jan 2025', 'Feb 2025', 'Mar 2025', 'Apr 2025', 'May 2025'],
'Value': [1351383, 1289531, 1246203, 1277814, 1279012, 1188546, 1474102, 1158332, 1269919, 1976531, 1415504, 1238846, 1342971, 1213585, 1274648, 1214250, 1204897]
}
df_monthly_trend = pd.DataFrame(monthly_data)
print("✅ Data for Monthly Trend prepared.")
# --- Chart 2 Data (Daily DNA Rate) ---
daily_data = {
'Day': ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'],
'DNA_Rate': [0.042183, 0.042488, 0.041213, 0.041781, 0.040324, 0.081696, 0.047208]
}
df_daily = pd.DataFrame(daily_data)
day_order = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
df_daily['Day'] = pd.Categorical(df_daily['Day'], categories=day_order, ordered=True)
df_daily = df_daily.sort_values('Day')
print("✅ Data for Daily DNA Rate prepared.")
# --- Chart 3 Data (Regional DNA Rate) ---
regional_data = {
'Region': ['London', 'North West', 'Midlands', 'South East', 'North East and Yorkshire', 'South West', 'East of England'],
'DNA_Rate': [0.055957, 0.050429, 0.040239, 0.039672, 0.036054, 0.033666, 0.032269]
}
df_regions = pd.DataFrame(regional_data).sort_values(by='DNA_Rate', ascending=False)
print("✅ Data for Regional DNA Rate prepared.")
# --- Part 2: Create and Combine the 3 Charts ---
# Define the layout: 1 chart on top, 2 on the bottom
fig = make_subplots(
rows=2, cols=2,
specs=[[{"colspan": 2}, None], [{}, {}]],
subplot_titles=(
"Monthly Missed Appointments Trend",
"DNA Rate by Day of the Week",
"DNA Rate by NHS Region"
)
)
# Create the individual figures to extract their traces
# CHANGED: The monthly trend chart is now a bar chart
fig1 = px.bar(df_monthly_trend, x='Month', y='Value', text='Value')
fig2 = px.bar(df_daily, x='Day', y='DNA_Rate', text='DNA_Rate')
fig3 = px.bar(df_regions, x='Region', y='DNA_Rate', text='DNA_Rate')
# Add traces from each figure to the subplot layout
for trace in fig1.data:
fig.add_trace(trace, row=1, col=1)
for trace in fig2.data:
fig.add_trace(trace, row=2, col=1)
for trace in fig3.data:
fig.add_trace(trace, row=2, col=2)
# --- Part 3: Format the Final Dashboard ---
fig.update_layout(
title_text="NHS Missed Appointments: A Multi-Faceted Analysis",
title_font_size=28,
plot_bgcolor='white',
height=800,
width=1400,
showlegend=False,
font=dict(family="Arial, sans-serif", size=12)
)
# Consistent color and formatting
fig.update_traces(
marker_color='#d9534f',
textposition='outside'
)
# Format axes and text labels
fig.update_yaxes(title_text="Number of DNAs", row=1, col=1)
fig.update_traces(texttemplate='%{text:.2s}', selector=dict(type='bar'), row=1, col=1)
fig.update_yaxes(title_text="Average DNA Rate", tickformat=".1%", row=2, col=1)
fig.update_traces(texttemplate='%{text:.1%}', selector=dict(type='bar'), row=2, col=1)
fig.update_yaxes(title_text="DNA Rate", tickformat=".1%", row=2, col=2)
fig.update_traces(texttemplate='%{text:.1%}', selector=dict(type='bar'), row=2, col=2)
# --- Part 4: Save the Final Image ---
try:
fig.write_image("NHS_Dashboard_All_Bars.png", scale=2)
print("\n✅ Dashboard image with all bar charts saved to 'NHS_Dashboard_All_Bars.png'")
except ValueError as e:
print(f"\n❌ Error saving image: {e}. Please ensure 'kaleido' is installed.")