ralate2 commited on
Commit
018460a
·
verified ·
1 Parent(s): 214fa70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -620,13 +620,30 @@ elif viz_type == "Complaints by Housing Block and Type":
620
  elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
621
  st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
622
 
623
- # Filtering the data based on the selected year and housing block
624
- filtered_data_time = data # Use filtered_data if date range is not needed
625
  if selected_year != 'All Time':
626
  filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
627
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
628
  # Further filtering by Housing Block (if applicable)
629
- if selected_block != 'All Blocks':
630
  filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
631
 
632
  # Pivoting the data based on the filtered data
@@ -642,15 +659,7 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
642
  complaint_pivot = complaint_pivot.astype(float)
643
 
644
  # Desired order for the housing blocks (up to '3300 block')
645
- desired_order = [
646
- '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
647
- '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
648
- '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
649
- '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
650
- '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
651
- '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
652
- '3200 block', '3300 block'
653
- ]
654
 
655
  # Reordering the index of the pivot table according to the desired order
656
  complaint_pivot = complaint_pivot.reindex(desired_order)
@@ -661,7 +670,7 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
661
  # Plotting the data
662
  fig, ax = plt.subplots(figsize=(10, 6))
663
 
664
- if selected_block != 'All Blocks':
665
  # Only show the selected housing block
666
  complaint_pivot.loc[[selected_block]].plot(kind='bar', stacked=True, colormap='inferno', ax=ax)
667
 
@@ -674,18 +683,20 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
674
  complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', ax=ax)
675
 
676
  # Adding percentage labels to the plot
677
- for idx, block in enumerate(complaint_pivot.index):
678
  cumulative_height = 0
679
- for i, complaint_type in enumerate(complaint_pivot.columns):
680
- count = complaint_pivot.iloc[idx, i]
681
- percent = percentages.iloc[idx, i]
 
682
  if count > 0:
683
  # Compute the position for the percentage label within each segment
684
- x_pos = idx # Adjusted to ensure it aligns with bar positions
685
  y_pos = cumulative_height + count / 2 # Center within each segment
 
686
  ax.text(
687
  x_pos, y_pos, f"{percent:.1f}%",
688
- ha='center', va='center',
689
  fontsize=10, color='black',
690
  bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
691
  )
 
620
  elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
621
  st.subheader("Complaints by Housing Block and Type - Incorporating Suggestions Based on Professor's Feedback")
622
 
623
+ # Filtering the data based on the selected year
624
+ filtered_data_time = data.copy() # Use filtered_data if date range is not needed
625
  if selected_year != 'All Time':
626
  filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
627
+
628
+ # Define available blocks (up to '3300 block')
629
+ available_blocks = [
630
+ '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
631
+ '600 block', '700 block', '800 block', '900 block', '1000 block',
632
+ '1100 block', '1200 block', '1300 block', '1400 block', '1500 block',
633
+ '1600 block', '1700 block', '1800 block', '1900 block',
634
+ '2000 block', '2100 block', '2200 block', '2300 block',
635
+ '2400 block', '2500 block', '2600 block',
636
+ '2700 block', '2800 block', '2900 block',
637
+ '3000 block', '3100 block',
638
+ '3200 block', '3300 block'
639
+ ]
640
+
641
+ # Ensure selected_block is in available_blocks
642
+ if selected_block not in available_blocks:
643
+ selected_block = None # or set to a default value
644
+
645
  # Further filtering by Housing Block (if applicable)
646
+ if selected_block and selected_block != 'All Blocks':
647
  filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
648
 
649
  # Pivoting the data based on the filtered data
 
659
  complaint_pivot = complaint_pivot.astype(float)
660
 
661
  # Desired order for the housing blocks (up to '3300 block')
662
+ desired_order = available_blocks
 
 
 
 
 
 
 
 
663
 
664
  # Reordering the index of the pivot table according to the desired order
665
  complaint_pivot = complaint_pivot.reindex(desired_order)
 
670
  # Plotting the data
671
  fig, ax = plt.subplots(figsize=(10, 6))
672
 
673
+ if selected_block and selected_block != 'All Blocks':
674
  # Only show the selected housing block
675
  complaint_pivot.loc[[selected_block]].plot(kind='bar', stacked=True, colormap='inferno', ax=ax)
676
 
 
683
  complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', ax=ax)
684
 
685
  # Adding percentage labels to the plot
686
+ for idx, (block, row) in enumerate(complaint_pivot.iterrows()):
687
  cumulative_height = 0
688
+ for i, complaint_type in enumerate(row):
689
+ count = row[i]
690
+ percent = percentages.loc[block, complaint_type] if count > 0 else 0
691
+
692
  if count > 0:
693
  # Compute the position for the percentage label within each segment
694
+ x_pos = idx if selected_block is None else 0 # Adjust x position based on selection
695
  y_pos = cumulative_height + count / 2 # Center within each segment
696
+
697
  ax.text(
698
  x_pos, y_pos, f"{percent:.1f}%",
699
+ ha='center', va='center',
700
  fontsize=10, color='black',
701
  bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
702
  )