icaoberg / cs-scholars2024-fair

CS Scholars 2024 Pre-college project
Apache License 2.0
0 stars 0 forks source link

Adding legend to group name #32

Closed icaoberg closed 3 months ago

icaoberg commented 3 months ago

Adding these two lines to the group_name plot it add a legend

# Create custom legend
legend_labels = top_10_value_counts.index.tolist()
ax.legend(wedges, legend_labels, title="Groups", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))
image

Adding a legend would make it more clear. The code should look similar to this block

# --------------------------------------------------------------------
# Calculate value counts and get the top 10 research group names
value_counts = df['group_name'].value_counts()
top_10_value_counts = value_counts.nlargest(10)
# Calculate "Others" category
others_count = value_counts.iloc[10:].sum()
if others_count > 0:
    top_10_value_counts['Others'] = others_count

# Plotting in Streamlit
fig, ax = plt.subplots(figsize=(10, 8))
wedges, texts, autotexts = ax.pie(top_10_value_counts,
                                  autopct='%1.1f%%',  # Add percentages
                                  explode=[0.1 if value == max(top_10_value_counts) else 0 for value in top_10_value_counts],  # Explode largest slice
                                  colors=plt.cm.tab20.colors[:len(top_10_value_counts)],  # Use tab20 colormap for colors
                                  shadow=True,  # Add shadow
                                  startangle=90,  # Rotate start angle
                                  textprops=dict(color="w"))  # Text color for percentages

ax.axis('equal')  # Equal aspect ratio ensures that pie is drawn as a circle.
# Create custom legend
legend_labels = top_10_value_counts.index.tolist()
ax.legend(wedges, legend_labels, title="Groups", loc="center left", bbox_to_anchor=(1, 0, 0.5, 1))

ax.set_title('Group names')  # Add title to the pie chart
# Display plot using Streamlit
st.pyplot(fig)
# --------------------------------------------------------------------
al3sandra commented 3 months ago

Pie chart was edited to only top 7 values and note was added in regards to that edit