Stacked Percentage Bar Plot in Matplotlib
0 2463
Visualizing Proportions: Stacked Percentage Bar Plots in Matplotlib
Stacked percentage bar plots are a powerful visualization tool that allows us to represent the relative proportions of different categories within a whole. Unlike traditional bar charts that display absolute values, stacked percentage bar plots normalize each bar to 100%, making it easier to compare the composition of different groups across categories. In this guide, we'll explore how to create stacked percentage bar plots using Matplotlib, a popular plotting library in Python.Understanding Stacked Percentage Bar Plots
A stacked percentage bar plot displays multiple datasets stacked on top of each other, with each bar representing 100% of the total for that category. The height of each segment within a bar corresponds to the percentage contribution of that dataset to the total. This type of plot is particularly useful when you want to compare the relative proportions of different categories across multiple groups.Creating a Basic Stacked Percentage Bar Plot
To create a stacked percentage bar plot, we need to follow these steps:- Prepare the Data: Organize your data into a format suitable for plotting. Typically, this involves creating a DataFrame where each column represents a different category, and each row represents a different group.
- Normalize the Data: Convert the absolute values into percentages by dividing each value by the total for that group and multiplying by 100.
- Plot the Data: Use Matplotlib's
bar()function to create the stacked bar plot, specifying thebottomparameter to stack the bars. - Customize the Plot: Add labels, titles, and legends to make the plot more informative.
import matplotlib.pyplot as plt
import numpy as np
# Data
categories = ['A', 'B', 'C', 'D']
y1 = np.array([10, 20, 10, 30])
y2 = np.array([20, 25, 15, 25])
y3 = np.array([12, 15, 19, 6])
y4 = np.array([10, 29, 13, 19])
# Normalize to percentages
total = y1 + y2 + y3 + y4
y1_percent = y1 / total * 100
y2_percent = y2 / total * 100
y3_percent = y3 / total * 100
y4_percent = y4 / total * 100
# Create stacked bar plot
plt.bar(categories, y1_percent, color='r')
plt.bar(categories, y2_percent, bottom=y1_percent, color='b')
plt.bar(categories, y3_percent, bottom=y1_percent + y2_percent, color='y')
plt.bar(categories, y4_percent, bottom=y1_percent + y2_percent + y3_percent, color='g')
# Add labels and title
plt.xlabel('Category')
plt.ylabel('Percentage')
plt.title('Stacked Percentage Bar Plot')
# Show plot
plt.show()
In this example, we first calculate the total for each category by summing the values of all datasets. Then, we compute the percentage for each dataset by dividing each value by the total and multiplying by 100. Finally, we create the stacked bar plot using Matplotlib's bar() function, stacking the bars by specifying the bottom parameter.
Adding Percentage Labels
To make the plot more informative, we can add percentage labels to each segment of the bars. This can be achieved by iterating over each bar and using thetext() function to place the labels at the appropriate positions:
bars = plt.bar(categories, y1_percent, color='r')
plt.bar(categories, y2_percent, bottom=y1_percent, color='b')
plt.bar(categories, y3_percent, bottom=y1_percent + y2_percent, color='y')
plt.bar(categories, y4_percent, bottom=y1_percent + y2_percent + y3_percent, color='g')
# Add labels
for i, bar in enumerate(bars):
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width() / 2, height / 2,
f'{height:.1f}%', ha='center', va='center', color='white')
plt.xlabel('Category')
plt.ylabel('Percentage')
plt.title('Stacked Percentage Bar Plot with Labels')
plt.show()
In this code, we iterate over each bar and use the get_height() method to retrieve the height of the bar, which corresponds to the percentage value. We then use the text() function to place the label at the center of the bar segment.
Customizing the Plot
Matplotlib provides various customization options to enhance the appearance of your stacked percentage bar plot:- Colors: Specify colors for each dataset using the
colorparameter in thebar()function. - Legends: Add a legend to differentiate between datasets using the
legend()function. - Gridlines: Enable gridlines for better readability using the
grid()function. - Bar Width: Adjust the width of the bars using the
widthparameter in thebar()function.
bars = plt.bar(categories, y1_percent, color='r', width=0.5)
plt.bar(categories, y2_percent, bottom=y1_percent, color='b', width=0.5)
plt.bar(categories, y3_percent, bottom=y1_percent + y2_percent, color='y', width=0.5)
plt.bar(categories, y4_percent, bottom=y1_percent + y2_percent + y3_percent, color='g', width=0.5)
# Add labels
for i, bar in enumerate(bars):
height = bar.get_height()
plt.text(bar.get_x() + bar.get_width() / 2, height / 2,
f'{height:.1f}%', ha='center', va='center', color='white')
plt.xlabel('Category')
plt.ylabel('Percentage')
plt.title('Customized Stacked Percentage Bar Plot')
# Show plot
plt.show()
In this example, we adjust the width of the bars to 0.5 to make them narrower and more spaced out. We also add a title and labels to the axes to provide context to the plot.
Conclusion
Stacked percentage bar plots are an effective way to visualize the composition of different categories within a whole. By normalizing the data to percentages, we can easily compare the relative proportions across different groups. Matplotlib provides a flexible and powerful platform to create these plots, offering a wide range of customization options to tailor the appearance to your needs.If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:



Comments
Waiting for your comments