ralate2 commited on
Commit
49bf2e8
·
verified ·
1 Parent(s): cde5e95

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -34
app.py CHANGED
@@ -416,44 +416,35 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
416
  st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
417
 
418
  # Filtering the data based on the selected year and housing block
419
- filtered_data_time = filtered_data # Use filtered_data if date range is not needed
 
420
  if selected_year != 'All Time':
421
  filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
422
 
423
- # Further filtering by Housing Block
424
  if selected_block != 'All Blocks':
425
  filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
426
 
427
- # Grouping data and pivot
428
- complaint_counts = filtered_data_time.groupby(['Housing Block', 'Type of Complaint']).size().reset_index(name='Count')
429
- complaint_counts['Housing Block'] = pd.Categorical(
430
- complaint_counts['Housing Block'],
431
- categories=desired_order,
432
- ordered=True
433
- )
434
-
435
- complaint_pivot = complaint_counts.pivot_table(
436
- index='Housing Block',
437
- columns='Type of Complaint',
438
- values='Count',
439
- aggfunc='sum',
440
  fill_value=0
441
  )
442
- complaint_pivot = complaint_pivot.reindex(desired_order)
 
 
 
 
443
  percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
444
 
445
- # Creating the plot
446
- fig, ax = plt.subplots(figsize=(10, 6))
447
- bar_width = 0.8
448
- complaint_pivot.plot(
449
- kind='bar',
450
- stacked=True,
451
- cmap='inferno',
452
- width=bar_width,
453
- ax=ax
454
- )
455
 
456
- # Adding percentage labels to the bars
 
457
  for idx, block in enumerate(complaint_pivot.index):
458
  cumulative_height = 0
459
  for i, complaint_type in enumerate(complaint_pivot.columns):
@@ -461,7 +452,7 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
461
  percent = percentages.iloc[idx, i]
462
  if count > 0:
463
  # Compute the position for the percentage label
464
- x_pos = idx - 0.4 + bar_width / 2
465
  y_pos = cumulative_height + count / 2
466
  ax.text(
467
  x_pos, y_pos, f"{percent:.1f}%",
@@ -471,18 +462,20 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
471
  )
472
  cumulative_height += count
473
 
 
474
  st.pyplot(fig)
475
 
 
476
  st.write("""
477
  **What this visualization shows:**
478
- This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the total number of complaints per block, categorized by type, with percentage labels displayed for each section. This allows for a quick comparison of the most common complaint types across different housing blocks and the relative proportion of each type.
479
-
480
  **Why it's interesting:**
481
- By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks. The percentage labels provide a clear view of the proportional representation of each complaint type within each block.
482
-
483
  **Color Scheme:**
484
- The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type, while the percentage labels offer a clearer understanding of the proportions.
485
- """)
486
 
487
 
488
  # Footer
 
416
  st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
417
 
418
  # Filtering the data based on the selected year and housing block
419
+ # Filtering the data based on the selected year and housing block
420
+ filtered_data_time = data # Use filtered_data if date range is not needed
421
  if selected_year != 'All Time':
422
  filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
423
 
424
+ # Further filtering by Housing Block (if applicable)
425
  if selected_block != 'All Blocks':
426
  filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
427
 
428
+ # Pivoting the data based on the filtered data
429
+ complaint_pivot = filtered_data_time.pivot_table(
430
+ index='Housing Block',
431
+ columns='Type of Complaint',
432
+ values='Disposition',
433
+ aggfunc='count',
 
 
 
 
 
 
 
434
  fill_value=0
435
  )
436
+
437
+ # Ensuring the pivoted data is numeric for plotting
438
+ complaint_pivot = complaint_pivot.astype(float)
439
+
440
+ # Calculating percentages for each complaint type per housing block
441
  percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
442
 
443
+ # Plotting the data
444
+ fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
 
 
 
 
 
 
 
 
445
 
446
+ # Adding percentage labels to the plot
447
+ ax = fig.gca()
448
  for idx, block in enumerate(complaint_pivot.index):
449
  cumulative_height = 0
450
  for i, complaint_type in enumerate(complaint_pivot.columns):
 
452
  percent = percentages.iloc[idx, i]
453
  if count > 0:
454
  # Compute the position for the percentage label
455
+ x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
456
  y_pos = cumulative_height + count / 2
457
  ax.text(
458
  x_pos, y_pos, f"{percent:.1f}%",
 
462
  )
463
  cumulative_height += count
464
 
465
+ # Display the plot in Streamlit
466
  st.pyplot(fig)
467
 
468
+ # Description of the visualization
469
  st.write("""
470
  **What this visualization shows:**
471
+ This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the total number of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks.
472
+
473
  **Why it's interesting:**
474
+ By analyzing the distribution of complaints by both block and type, organizations can identify specific areas where certain complaint types are more prevalent. This insight helps target interventions and allocate resources more efficiently based on the most common issues in different housing blocks.
475
+
476
  **Color Scheme:**
477
+ The 'inferno' color palette is used to represent different complaint types, with darker shades indicating a higher frequency of complaints. The stacked bar chart makes it easy to compare the distribution of complaints by block and type.
478
+ """)
479
 
480
 
481
  # Footer