Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
|
| 439 |
-
|
| 440 |
|
| 441 |
-
#
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
|
| 446 |
-
#
|
| 447 |
-
|
| 448 |
-
|
| 449 |
|
| 450 |
-
#
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
|
| 459 |
-
#
|
| 460 |
-
|
| 461 |
|
| 462 |
-
#
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
|
| 476 |
-
#
|
| 477 |
-
|
| 478 |
|
| 479 |
-
#
|
| 480 |
-
|
| 481 |
|
| 482 |
-
#
|
| 483 |
-
|
| 484 |
|
| 485 |
-
#
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
#
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
|
| 504 |
-
#
|
| 505 |
-
|
| 506 |
|
| 507 |
-
#
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
|
| 512 |
-
|
| 513 |
-
|
| 514 |
|
| 515 |
-
|
| 516 |
-
|
| 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,105 +617,6 @@ elif viz_type == "Complaints by Housing Block and Type":
|
|
| 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
|
| 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
|
| 650 |
-
complaint_pivot = filtered_data_time.pivot_table(
|
| 651 |
-
index='Housing Block',
|
| 652 |
-
columns='Type of Complaint',
|
| 653 |
-
values='Disposition',
|
| 654 |
-
aggfunc='count',
|
| 655 |
-
fill_value=0
|
| 656 |
-
)
|
| 657 |
-
|
| 658 |
-
# Ensuring the pivoted data is numeric for plotting
|
| 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)
|
| 666 |
-
|
| 667 |
-
# Calculating percentages for each complaint type per housing block
|
| 668 |
-
percentages = complaint_pivot.div(complaint_pivot.sum(axis=1), axis=0) * 100
|
| 669 |
-
|
| 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 |
-
|
| 677 |
-
# Set x-ticks to only show the selected housing block
|
| 678 |
-
ax.set_xticks([0])
|
| 679 |
-
ax.set_xticklabels([selected_block])
|
| 680 |
-
|
| 681 |
-
else:
|
| 682 |
-
# Show all blocks up to 3300
|
| 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 |
-
)
|
| 703 |
-
cumulative_height += count
|
| 704 |
-
|
| 705 |
-
# Display the plot in Streamlit
|
| 706 |
-
st.pyplot(fig)
|
| 707 |
-
|
| 708 |
-
# Writeup
|
| 709 |
-
st.write("""
|
| 710 |
-
**What this visualization shows:**
|
| 711 |
-
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.
|
| 712 |
-
|
| 713 |
-
**Why it's interesting:**
|
| 714 |
-
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.
|
| 715 |
-
|
| 716 |
-
**Color Scheme:**
|
| 717 |
-
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.
|
| 718 |
-
""")
|
| 719 |
|
| 720 |
# Footer
|
| 721 |
st.markdown("---")
|
|
|
|
| 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 |
|
| 621 |
# Footer
|
| 622 |
st.markdown("---")
|