Spaces:
Runtime error
Runtime error
Update visualizations.py
Browse files- visualizations.py +38 -48
visualizations.py
CHANGED
|
@@ -9,14 +9,13 @@ from data_processor import DataProcessor
|
|
| 9 |
def create_visualizations(data_processor: DataProcessor) -> Dict[str, Any]:
|
| 10 |
"""
|
| 11 |
Create all visualizations for the Fetii dashboard.
|
| 12 |
-
Compatible with both Streamlit and Gradio interfaces.
|
| 13 |
"""
|
| 14 |
insights = data_processor.get_quick_insights()
|
| 15 |
df = data_processor.df
|
| 16 |
|
| 17 |
visualizations = {}
|
| 18 |
|
| 19 |
-
# Core visualizations
|
| 20 |
visualizations['hourly_distribution'] = create_hourly_chart(insights['hourly_distribution'])
|
| 21 |
visualizations['group_size_distribution'] = create_group_size_chart(insights['group_size_distribution'])
|
| 22 |
visualizations['popular_locations'] = create_locations_chart(insights['top_pickups'])
|
|
@@ -49,21 +48,21 @@ def create_hourly_chart(hourly_data: Dict[int, int]) -> go.Figure:
|
|
| 49 |
|
| 50 |
fig = go.Figure()
|
| 51 |
|
| 52 |
-
# Create
|
| 53 |
max_count = max(counts)
|
| 54 |
colors = []
|
| 55 |
for count in counts:
|
| 56 |
intensity = count / max_count
|
| 57 |
if intensity > 0.8:
|
| 58 |
-
colors.append('#
|
| 59 |
elif intensity > 0.6:
|
| 60 |
-
colors.append('#
|
| 61 |
elif intensity > 0.4:
|
| 62 |
-
colors.append('#
|
| 63 |
elif intensity > 0.2:
|
| 64 |
-
colors.append('#
|
| 65 |
else:
|
| 66 |
-
colors.append('#
|
| 67 |
|
| 68 |
fig.add_trace(go.Bar(
|
| 69 |
x=hour_labels,
|
|
@@ -83,15 +82,15 @@ def create_hourly_chart(hourly_data: Dict[int, int]) -> go.Figure:
|
|
| 83 |
title={
|
| 84 |
'text': 'Trip Distribution by Hour',
|
| 85 |
'x': 0.5,
|
| 86 |
-
'font': {'size':
|
| 87 |
},
|
| 88 |
xaxis_title='Hour of Day',
|
| 89 |
yaxis_title='Number of Trips',
|
| 90 |
plot_bgcolor='rgba(0,0,0,0)',
|
| 91 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 92 |
font={'color': '#374151', 'family': 'Inter'},
|
| 93 |
-
height=
|
| 94 |
-
margin=dict(t=
|
| 95 |
xaxis=dict(
|
| 96 |
showgrid=True,
|
| 97 |
gridwidth=1,
|
|
@@ -115,11 +114,11 @@ def create_group_size_chart(group_data: Dict[int, int]) -> go.Figure:
|
|
| 115 |
sizes = list(group_data.keys())
|
| 116 |
counts = list(group_data.values())
|
| 117 |
|
| 118 |
-
#
|
| 119 |
colors = [
|
| 120 |
-
'#
|
| 121 |
-
'#
|
| 122 |
-
'#
|
| 123 |
]
|
| 124 |
|
| 125 |
fig = go.Figure()
|
|
@@ -134,22 +133,30 @@ def create_group_size_chart(group_data: Dict[int, int]) -> go.Figure:
|
|
| 134 |
hovertemplate='<b>%{label}</b><br>Trips: %{value}<br>Percentage: %{percent}<extra></extra>',
|
| 135 |
textinfo='label+percent',
|
| 136 |
textposition='auto',
|
| 137 |
-
textfont=dict(color='white', size=
|
| 138 |
-
hole=0.
|
| 139 |
))
|
| 140 |
|
| 141 |
fig.update_layout(
|
| 142 |
title={
|
| 143 |
'text': 'Group Size Distribution',
|
| 144 |
'x': 0.5,
|
| 145 |
-
'font': {'size':
|
| 146 |
},
|
| 147 |
plot_bgcolor='rgba(0,0,0,0)',
|
| 148 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 149 |
font={'color': '#374151', 'family': 'Inter'},
|
| 150 |
-
height=
|
| 151 |
-
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
)
|
| 154 |
|
| 155 |
return fig
|
|
@@ -169,17 +176,12 @@ def create_locations_chart(pickup_data: list) -> go.Figure:
|
|
| 169 |
|
| 170 |
fig = go.Figure()
|
| 171 |
|
| 172 |
-
#
|
| 173 |
max_count = max(counts)
|
| 174 |
-
base_colors = ['#667eea', '#764ba2', '#f093fb', '#f5576c', '#4facfe', '#00f2fe', '#43e97b', '#38f9d7']
|
| 175 |
colors = []
|
| 176 |
-
for
|
| 177 |
-
base_color = base_colors[i % len(base_colors)]
|
| 178 |
-
# Convert hex to rgba with opacity based on intensity
|
| 179 |
-
hex_color = base_color.lstrip('#')
|
| 180 |
-
rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))
|
| 181 |
intensity = count / max_count
|
| 182 |
-
colors.append(f'rgba(
|
| 183 |
|
| 184 |
fig.add_trace(go.Bar(
|
| 185 |
x=counts,
|
|
@@ -201,15 +203,15 @@ def create_locations_chart(pickup_data: list) -> go.Figure:
|
|
| 201 |
title={
|
| 202 |
'text': 'Top Pickup Locations',
|
| 203 |
'x': 0.5,
|
| 204 |
-
'font': {'size':
|
| 205 |
},
|
| 206 |
xaxis_title='Number of Pickups',
|
| 207 |
yaxis_title='',
|
| 208 |
plot_bgcolor='rgba(0,0,0,0)',
|
| 209 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 210 |
font={'color': '#374151', 'family': 'Inter'},
|
| 211 |
-
height=
|
| 212 |
-
margin=dict(t=
|
| 213 |
yaxis=dict(
|
| 214 |
autorange="reversed",
|
| 215 |
showline=True,
|
|
@@ -273,27 +275,15 @@ def create_time_heatmap(df: pd.DataFrame) -> go.Figure:
|
|
| 273 |
title={
|
| 274 |
'text': 'Trip Patterns by Day & Hour',
|
| 275 |
'x': 0.5,
|
| 276 |
-
'font': {'size':
|
| 277 |
},
|
| 278 |
xaxis_title='Hour of Day',
|
| 279 |
yaxis_title='Day of Week',
|
| 280 |
-
plot_bgcolor='rgba(
|
| 281 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 282 |
font={'color': '#374151', 'family': 'Inter'},
|
| 283 |
-
height=
|
| 284 |
-
margin=dict(t=
|
| 285 |
-
xaxis=dict(
|
| 286 |
-
showgrid=True,
|
| 287 |
-
gridwidth=1,
|
| 288 |
-
gridcolor='rgba(156, 163, 175, 0.3)',
|
| 289 |
-
tickfont=dict(size=11)
|
| 290 |
-
),
|
| 291 |
-
yaxis=dict(
|
| 292 |
-
showgrid=True,
|
| 293 |
-
gridwidth=1,
|
| 294 |
-
gridcolor='rgba(156, 163, 175, 0.3)',
|
| 295 |
-
tickfont=dict(size=11)
|
| 296 |
-
)
|
| 297 |
)
|
| 298 |
|
| 299 |
return fig
|
|
|
|
| 9 |
def create_visualizations(data_processor: DataProcessor) -> Dict[str, Any]:
|
| 10 |
"""
|
| 11 |
Create all visualizations for the Fetii dashboard.
|
|
|
|
| 12 |
"""
|
| 13 |
insights = data_processor.get_quick_insights()
|
| 14 |
df = data_processor.df
|
| 15 |
|
| 16 |
visualizations = {}
|
| 17 |
|
| 18 |
+
# Core visualizations
|
| 19 |
visualizations['hourly_distribution'] = create_hourly_chart(insights['hourly_distribution'])
|
| 20 |
visualizations['group_size_distribution'] = create_group_size_chart(insights['group_size_distribution'])
|
| 21 |
visualizations['popular_locations'] = create_locations_chart(insights['top_pickups'])
|
|
|
|
| 48 |
|
| 49 |
fig = go.Figure()
|
| 50 |
|
| 51 |
+
# Create gradient colors based on intensity
|
| 52 |
max_count = max(counts)
|
| 53 |
colors = []
|
| 54 |
for count in counts:
|
| 55 |
intensity = count / max_count
|
| 56 |
if intensity > 0.8:
|
| 57 |
+
colors.append('#dc2626') # Red for peak
|
| 58 |
elif intensity > 0.6:
|
| 59 |
+
colors.append('#ea580c') # Orange-red
|
| 60 |
elif intensity > 0.4:
|
| 61 |
+
colors.append('#d97706') # Orange
|
| 62 |
elif intensity > 0.2:
|
| 63 |
+
colors.append('#3b82f6') # Blue
|
| 64 |
else:
|
| 65 |
+
colors.append('#6b7280') # Gray for low activity
|
| 66 |
|
| 67 |
fig.add_trace(go.Bar(
|
| 68 |
x=hour_labels,
|
|
|
|
| 82 |
title={
|
| 83 |
'text': 'Trip Distribution by Hour',
|
| 84 |
'x': 0.5,
|
| 85 |
+
'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
|
| 86 |
},
|
| 87 |
xaxis_title='Hour of Day',
|
| 88 |
yaxis_title='Number of Trips',
|
| 89 |
plot_bgcolor='rgba(0,0,0,0)',
|
| 90 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 91 |
font={'color': '#374151', 'family': 'Inter'},
|
| 92 |
+
height=320,
|
| 93 |
+
margin=dict(t=60, b=50, l=50, r=50),
|
| 94 |
xaxis=dict(
|
| 95 |
showgrid=True,
|
| 96 |
gridwidth=1,
|
|
|
|
| 114 |
sizes = list(group_data.keys())
|
| 115 |
counts = list(group_data.values())
|
| 116 |
|
| 117 |
+
# Modern color palette
|
| 118 |
colors = [
|
| 119 |
+
'#3b82f6', '#10b981', '#f59e0b', '#ef4444',
|
| 120 |
+
'#8b5cf6', '#06b6d4', '#84cc16', '#f97316',
|
| 121 |
+
'#ec4899', '#6366f1', '#14b8a6', '#eab308'
|
| 122 |
]
|
| 123 |
|
| 124 |
fig = go.Figure()
|
|
|
|
| 133 |
hovertemplate='<b>%{label}</b><br>Trips: %{value}<br>Percentage: %{percent}<extra></extra>',
|
| 134 |
textinfo='label+percent',
|
| 135 |
textposition='auto',
|
| 136 |
+
textfont=dict(color='white', size=14, family='Inter'),
|
| 137 |
+
hole=0.3 # Reduced hole size to make chart appear larger
|
| 138 |
))
|
| 139 |
|
| 140 |
fig.update_layout(
|
| 141 |
title={
|
| 142 |
'text': 'Group Size Distribution',
|
| 143 |
'x': 0.5,
|
| 144 |
+
'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
|
| 145 |
},
|
| 146 |
plot_bgcolor='rgba(0,0,0,0)',
|
| 147 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 148 |
font={'color': '#374151', 'family': 'Inter'},
|
| 149 |
+
height=500, # Increased height from 320 to 500
|
| 150 |
+
width=500, # Added explicit width
|
| 151 |
+
margin=dict(t=60, b=50, l=50, r=50),
|
| 152 |
+
showlegend=True, # Show legend to make it more informative
|
| 153 |
+
legend=dict(
|
| 154 |
+
orientation="v",
|
| 155 |
+
yanchor="middle",
|
| 156 |
+
y=0.5,
|
| 157 |
+
xanchor="left",
|
| 158 |
+
x=1.05
|
| 159 |
+
)
|
| 160 |
)
|
| 161 |
|
| 162 |
return fig
|
|
|
|
| 176 |
|
| 177 |
fig = go.Figure()
|
| 178 |
|
| 179 |
+
# Gradient colors
|
| 180 |
max_count = max(counts)
|
|
|
|
| 181 |
colors = []
|
| 182 |
+
for count in counts:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
intensity = count / max_count
|
| 184 |
+
colors.append(f'rgba(59, 130, 246, {0.4 + intensity * 0.6})')
|
| 185 |
|
| 186 |
fig.add_trace(go.Bar(
|
| 187 |
x=counts,
|
|
|
|
| 203 |
title={
|
| 204 |
'text': 'Top Pickup Locations',
|
| 205 |
'x': 0.5,
|
| 206 |
+
'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
|
| 207 |
},
|
| 208 |
xaxis_title='Number of Pickups',
|
| 209 |
yaxis_title='',
|
| 210 |
plot_bgcolor='rgba(0,0,0,0)',
|
| 211 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 212 |
font={'color': '#374151', 'family': 'Inter'},
|
| 213 |
+
height=320,
|
| 214 |
+
margin=dict(t=60, b=50, l=140, r=50),
|
| 215 |
yaxis=dict(
|
| 216 |
autorange="reversed",
|
| 217 |
showline=True,
|
|
|
|
| 275 |
title={
|
| 276 |
'text': 'Trip Patterns by Day & Hour',
|
| 277 |
'x': 0.5,
|
| 278 |
+
'font': {'size': 18, 'color': '#1f2937', 'family': 'Inter'}
|
| 279 |
},
|
| 280 |
xaxis_title='Hour of Day',
|
| 281 |
yaxis_title='Day of Week',
|
| 282 |
+
plot_bgcolor='rgba(0,0,0,0)',
|
| 283 |
paper_bgcolor='rgba(0,0,0,0)',
|
| 284 |
font={'color': '#374151', 'family': 'Inter'},
|
| 285 |
+
height=400,
|
| 286 |
+
margin=dict(t=60, b=50, l=100, r=50)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
)
|
| 288 |
|
| 289 |
return fig
|