ralate2 commited on
Commit
1cbfe7e
·
verified ·
1 Parent(s): 858b949

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +155 -67
app.py CHANGED
@@ -435,86 +435,86 @@ elif viz_type == "Complaints by Housing Block and Type":
435
  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.
436
  """)
437
 
438
- elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
439
- st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
440
 
441
- # Filtering the data based on the selected year and housing block
442
- filtered_data_time = data # Use filtered_data if date range is not needed
443
- if selected_year != 'All Time':
444
- filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
445
 
446
- # Further filtering by Housing Block (if applicable)
447
- if selected_block != 'All Blocks':
448
- filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
449
 
450
- # Pivoting the data based on the filtered data
451
- complaint_pivot = filtered_data_time.pivot_table(
452
- index='Housing Block',
453
- columns='Type of Complaint',
454
- values='Disposition',
455
- aggfunc='count',
456
- fill_value=0
457
- )
458
 
459
- # Ensuring the pivoted data is numeric for plotting
460
- complaint_pivot = complaint_pivot.astype(float)
461
 
462
- # Desired order for the housing blocks
463
- desired_order = [
464
- '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
465
- '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
466
- '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
467
- '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
468
- '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
469
- '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
470
- '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
471
- '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
472
- '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
473
- '4700 block', '4800 block', '4900 block', '5000 block'
474
- ]
475
 
476
- # Reordering the index of the pivot table according to the desired order
477
- complaint_pivot = complaint_pivot.reindex(desired_order)
478
 
479
- # Calculating percentages for each complaint type per housing block
480
- percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
481
 
482
- # Plotting the data
483
- fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
484
 
485
- # Adding percentage labels to the plot
486
- ax = fig.gca()
487
- for idx, block in enumerate(complaint_pivot.index):
488
- cumulative_height = 0
489
- for i, complaint_type in enumerate(complaint_pivot.columns):
490
- count = complaint_pivot.iloc[idx, i]
491
- percent = percentages.iloc[idx, i]
492
- if count > 0:
493
- # Compute the position for the percentage label
494
- x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
495
- y_pos = cumulative_height + count / 2
496
- ax.text(
497
- x_pos, y_pos, f"{percent:.1f}%",
498
- ha='center', va='center',
499
- fontsize=10, color='black',
500
- bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
501
- )
502
- cumulative_height += count
503
 
504
- # Display the plot in Streamlit
505
- st.pyplot(fig)
506
 
507
- # writeup
508
- st.write("""
509
- **What this visualization shows:**
510
- This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
511
 
512
- **Why it's interesting:**
513
- 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.
514
 
515
- **Color Scheme:**
516
- 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.
517
- """)
518
  # In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
519
 
520
  # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
@@ -617,6 +617,94 @@ elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestion
617
  # 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.
618
  # """)
619
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
620
  # Footer
621
  st.markdown("---")
622
  st.markdown("Dataset provided by the City of Urbana Open Data Portal - https://data.urbanaillinois.us/Environment/Nuisance-Complaints/tsn9-95m3/about_data ")
 
435
  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.
436
  """)
437
 
438
+ # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
439
+ # st.subheader("Complaints by Housing Block and Type- Incorporating Suggestions Based on Professor's Feedback")
440
 
441
+ # # Filtering the data based on the selected year and housing block
442
+ # filtered_data_time = data # Use filtered_data if date range is not needed
443
+ # if selected_year != 'All Time':
444
+ # filtered_data_time = filtered_data_time[filtered_data_time['Year Reported'] == selected_year]
445
 
446
+ # # Further filtering by Housing Block (if applicable)
447
+ # if selected_block != 'All Blocks':
448
+ # filtered_data_time = filtered_data_time[filtered_data_time['Housing Block'] == selected_block]
449
 
450
+ # # Pivoting the data based on the filtered data
451
+ # complaint_pivot = filtered_data_time.pivot_table(
452
+ # index='Housing Block',
453
+ # columns='Type of Complaint',
454
+ # values='Disposition',
455
+ # aggfunc='count',
456
+ # fill_value=0
457
+ # )
458
 
459
+ # # Ensuring the pivoted data is numeric for plotting
460
+ # complaint_pivot = complaint_pivot.astype(float)
461
 
462
+ # # Desired order for the housing blocks
463
+ # desired_order = [
464
+ # '1 block', '100 block', '200 block', '300 block', '400 block', '500 block',
465
+ # '600 block', '700 block', '800 block', '900 block', '1000 block', '1100 block',
466
+ # '1200 block', '1300 block', '1400 block', '1500 block', '1600 block',
467
+ # '1700 block', '1800 block', '1900 block', '2000 block', '2100 block',
468
+ # '2200 block', '2300 block', '2400 block', '2500 block', '2600 block',
469
+ # '2700 block', '2800 block', '2900 block', '3000 block', '3100 block',
470
+ # '3200 block', '3300 block', '3400 block', '3500 block', '3600 block',
471
+ # '3700 block', '3800 block', '3900 block', '4000 block', '4100 block',
472
+ # '4200 block', '4300 block', '4400 block', '4500 block', '4600 block',
473
+ # '4700 block', '4800 block', '4900 block', '5000 block'
474
+ # ]
475
 
476
+ # # Reordering the index of the pivot table according to the desired order
477
+ # complaint_pivot = complaint_pivot.reindex(desired_order)
478
 
479
+ # # Calculating percentages for each complaint type per housing block
480
+ # percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
481
 
482
+ # # Plotting the data
483
+ # fig = complaint_pivot.plot(kind='bar', stacked=True, colormap='inferno', figsize=(10, 6)).get_figure()
484
 
485
+ # # Adding percentage labels to the plot
486
+ # ax = fig.gca()
487
+ # for idx, block in enumerate(complaint_pivot.index):
488
+ # cumulative_height = 0
489
+ # for i, complaint_type in enumerate(complaint_pivot.columns):
490
+ # count = complaint_pivot.iloc[idx, i]
491
+ # percent = percentages.iloc[idx, i]
492
+ # if count > 0:
493
+ # # Compute the position for the percentage label
494
+ # x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
495
+ # y_pos = cumulative_height + count / 2
496
+ # ax.text(
497
+ # x_pos, y_pos, f"{percent:.1f}%",
498
+ # ha='center', va='center',
499
+ # fontsize=10, color='black',
500
+ # bbox=dict(facecolor='white', alpha=0.7, edgecolor='none')
501
+ # )
502
+ # cumulative_height += count
503
 
504
+ # # Display the plot in Streamlit
505
+ # st.pyplot(fig)
506
 
507
+ # # writeup
508
+ # st.write("""
509
+ # **What this visualization shows:**
510
+ # This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
511
 
512
+ # **Why it's interesting:**
513
+ # 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.
514
 
515
+ # **Color Scheme:**
516
+ # 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.
517
+ # """)
518
  # In the above code , We incorporated all of the professor's suggestions and refined the chart to make it more useful for analysis while ensuring good aesthetics. Given that the data from block 3400 onwards is very sparse, we decided to exclude these records. This adjustment helped focus the visualization on the more relevant data, providing clearer insights and improving its overall effectiveness for analysis.
519
 
520
  # elif viz_type == "Complaints by Housing Block and Type (Incorporating Suggestions Based on Professor's Feedback)":
 
617
  # 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.
618
  # """)
619
 
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
633
+ complaint_pivot = filtered_data_time.pivot_table(
634
+ index='Housing Block',
635
+ columns='Type of Complaint',
636
+ values='Disposition',
637
+ aggfunc='count',
638
+ fill_value=0
639
+ )
640
+
641
+ # Ensuring the pivoted data is numeric for plotting
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)
657
+
658
+ # Calculating percentages for each complaint type per housing block
659
+ percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
660
+
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
+
668
+ # Set x-ticks to only show the selected housing block
669
+ ax.set_xticks([0])
670
+ ax.set_xticklabels([selected_block])
671
+
672
+ else:
673
+ # Show all blocks up to 3300
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
684
+ x_pos = idx - 0.4 + 0.8 / 2 # Adjusting the position of the label
685
+ y_pos = cumulative_height + count / 2
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
+ )
692
+ cumulative_height += count
693
+
694
+ # Display the plot in Streamlit
695
+ st.pyplot(fig)
696
+
697
+ # Writeup
698
+ st.write("""
699
+ **What this visualization shows:**
700
+ This bar chart displays the distribution of complaints by Housing Block and Complaint Type. The data is stacked to show the percentage of complaints per block, categorized by type. This allows for a quick comparison of the most common complaint types across different housing blocks. While the percentages may be challenging to read when data for all blocks is displayed, they become more valuable and easier to interpret when a single block is selected. Selecting a specific block allows for clearer insights into the proportion of each complaint type within that block, providing more actionable information.
701
+
702
+ **Why it's interesting:**
703
+ 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.
704
+
705
+ **Color Scheme:**
706
+ 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.
707
+ """)
708
  # Footer
709
  st.markdown("---")
710
  st.markdown("Dataset provided by the City of Urbana Open Data Portal - https://data.urbanaillinois.us/Environment/Nuisance-Complaints/tsn9-95m3/about_data ")