Professional Documents
Culture Documents
Count of (1, 0, N) For Each Category in Pandas - Plotting Each Category in Separate Subplots
Count of (1, 0, N) For Each Category in Pandas - Plotting Each Category in Separate Subplots
"Survived," counting the occurrences, and then creating a stacked bar chart for the
top five combinations with the highest count of "Survived" equal to 1. Here's the
python
Copy code
result = df.groupby(["Prefix",
"Survived"]).size().unstack(fill_value=0).sort_values(by=1,
ascending=False).head()
category_data = result.loc[[category]]
# Plot a stacked bar chart on the current axis
# Set title, x-axis label, y-axis label, and legend for the current axis
ax.set_title(f'Category {category}')
ax.set_xlabel('Prefix')
ax.set_ylabel('Count')
ax.legend(title='Prefix')
plt.tight_layout()
plt.show()
Save to grepper
This code creates a 2x3 grid of subplots, where each subplot represents a category.
It then iterates through the categories, extracts the data for each category, and plots
a stacked bar chart on the corresponding subplot. The titles, labels, and legends are
set for each subplot, and the layout is adjusted for better spacing before displaying
the plot.