Creating a Stacked Bar Chart with Matplotlib in Python
Data visualization is an essential part of data analysis in today’s world. It allows us to present complex data in a simplified form that is easy to understand and analyze.
In data visualization, charts and graphs are the most commonly used tools. In this article, we will focus on creating a stacked bar chart using the Matplotlib library in Python.
We will discuss the basics of chart creation, how to add titles, labels, and legends, and then customize our chart’s colors. Lastly, we will cover data and chart parameters, including how to define and use them to create an informative and visually appealing chart.
1) Creating a Stacked Bar Chart:
The stacked bar chart is a type of chart that displays the comparison of multiple variables in a single chart by stacking them on top of each other. This way, we can quickly see the total size of each stack while also viewing the component parts.
To create a stacked bar chart using Matplotlib, we first need to import the library and generate our data. We can use the matplotlib.pyplot.bar()
function to create our stacked chart.
Suppose we have data for book sales in three different regions: North America, Europe, and Asia. The data for each region is further divided into book categories: Fiction, Non-Fiction, and Poetry.
We can represent this data using a stacked bar chart. Here is the code to create a basic stacked bar chart:
import matplotlib.pyplot as plt
North_America = [20, 34, 25]
Europe = [33, 20, 10]
Asia = [45, 15, 22]
plt.bar(range(len(North_America)), North_America, label='North America')
plt.bar(range(len(Europe)), Europe, bottom=North_America, label='Europe')
plt.bar(range(len(Asia)), Asia, bottom=[i + j for i, j in zip(North_America, Europe)],
label='Asia')
plt.title('Book Sales by Region and Category')
plt.xlabel('Book Category')
plt.ylabel('Sales')
plt.xticks(range(len(North_America)), ['Fiction', 'Non-Fiction', 'Poetry'])
plt.legend(loc='upper left')
plt.show()
The plt.bar()
function takes two parameters: the first parameter is an array that defines the x-axis values, and the second parameter is an array that defines the y-axis values.
In this chart, we have three sets of data representing each region. We use three calls to plt.bar()
, one for each region, to display the data.
The bottom
parameter is used to specify the bottom of each bar, i.e., where the previous bar’s top ends.
2) Adding Title, Labels, and Legend:
To create an informative stacked bar chart, we need to provide a title, labels, and a legend.
A title describes the chart’s purpose, labels provide a description for each axis, and a legend identifies the data represented in the chart. Here is how we can add these elements to our chart:
plt.title('Book Sales by Region and Category')
plt.xlabel('Book Category')
plt.ylabel('Sales')
plt.xticks(range(len(North_America)), ['Fiction', 'Non-Fiction', 'Poetry'])
plt.legend(loc='upper left')
plt.show()
The title()
function sets the title of the chart, xlabel()
and ylabel()
function set the x-axis and y-axis labels, respectively.
The xticks()
function sets the label for each x-axis value. Finally, legend()
sets the legend for the chart.
3) Customizing Chart Colors:
Custom chart colors are an excellent way to make a chart visually appealing and stand out. We can customize the color of each bar in the chart using Matplotlib’s plt.bar()
function.
Here is how we can customize the colors of the bars in our stacked bar chart:
North_America = [20, 34, 25]
Europe = [33, 20, 10]
Asia = [45, 15, 22]
plt.bar(range(len(North_America)), North_America, color='red', label='North America')
plt.bar(range(len(Europe)), Europe, bottom=North_America, color='blue', label='Europe')
plt.bar(range(len(Asia)), Asia, bottom=[i + j for i, j in zip(North_America, Europe)],
color='green', label='Asia')
plt.title('Book Sales by Region and Category')
plt.xlabel('Book Category')
plt.ylabel('Sales')
plt.xticks(range(len(North_America)), ['Fiction', 'Non-Fiction', 'Poetry'])
plt.legend(loc='upper left')
plt.show()
Here, we have added the color
parameter in each call to plt.bar()
to customize the colors of the bars. You can change these colors to any of your choice, and even use gradients or color palettes to highlight particular data.
4) Data and Chart Parameters:
Data and chart parameters are essential factors to consider when creating any data visualization because they allow you to fine-tune the chart to convey specific information. Let us go through some of the most important parameters to keep in mind while creating stacked bar charts.
– Defining Data for the Chart:
Data is the fundamental aspect of creating any chart. In a stacked bar chart, we need to define data for each bar and category.
We can do it by adding a new array for each category and adding the corresponding values. – Defining Chart Parameters:
Chart parameters are critical to control the chart axes, bars, and spacing.
Defining chart parameters can help in creating a cleaner and more straightforward chart, which is easier to interpret. Some critical parameters include the number of bars per category (N), the width of the bars (barWidth), and the horizontal location of the bars (xloc).
The Bottom Line:
Creating a stacked bar chart using Matplotlib in Python is a simple yet powerful way to visually analyze data that is divided into categories and subcategories. With basic chart creation, customization, and data and chart parameter classification, creating an informative and visually appealing chart has become more accessible than ever.
3) Displaying the Stacked Bar Chart:
Once the stacked bar chart is created, we need to display it using Matplotlib.
Matplotlib provides several functions to display the chart in a Jupyter notebook, on an interactive website, or save it as an image file.
Here is how we can display our stacked bar chart using Matplotlib:
plt.show()
The show()
function is all we need to display the chart.
When executed, it will open a window and display the chart. We can also save the chart as an image to use in other documents or export it as a SVG or PDF file.
Here is the code to save the chart as a PNG file:
plt.savefig('book_sales_chart.png')
This will save the chart as a PNG file in the same directory as the Python script. You can also provide the full path to save the image to a specific directory.
4) Additional Resources:
Matplotlib is a powerful library with a steep learning curve. Fortunately, there are many resources available that can help you learn how to create various charts using Matplotlib.
Here are some of the best resources:
- – Matplotlib Documentation: The official Matplotlib documentation is the best place to start learning about the library. It contains a wealth of information on how to use Matplotlib, including detailed documentation on each function and many examples.
- – Matplotlib Tutorials: The Matplotlib website also provides a set of tutorials that cover the basics of using the library. These tutorials include step-by-step instructions on how to create various types of charts, including stacked bar charts.
- – Data Visualization with Matplotlib: A Complete Guide: This book by Benjamin Walter Keller is an excellent resource for learning how to create various charts using Matplotlib. The book provides detailed explanations of each chart type, including how to customize them and use advanced features in Matplotlib.
- – Python Plotting with Matplotlib (Guide): This guide on Real Python provides an in-depth introduction to Matplotlib and covers everything from basic charts to advanced visualizations. – Stack Overflow: Stack Overflow is a popular question and answer website where developers can post questions and get answers from the community.
- – Stack Overflow: Stack Overflow is a popular question and answer website where developers can post questions and get answers from the community. It is a great resource for finding solutions to specific problems, including Matplotlib-related issues.
Conclusion:
Creating a stacked bar chart using Matplotlib in Python allows us to present complex data in a simplified form that is easy to understand and analyze.
With the basic chart creation, customization, data and chart parameter classification, display options, and available resources, creating an informative and visually appealing chart has never been easier. Matplotlib is a powerful library with many features, which can take some time to master.
However, with the right resources and practice, anyone can create stunning charts that unlock exciting insights from their data. In conclusion, creating a stacked bar chart using Matplotlib in Python is a powerful tool that allows us to visually analyze complex data.
In summary, we covered the basics of chart creation, adding titles, labels, and legends, customizing chart colors, defining data and chart parameters, and displaying the chart using Matplotlib. The availability of resources ensures that anyone can learn Matplotlib and create visually appealing charts that unlock exciting insights from their data.
Therefore, it is crucial to master this skill and incorporate it into your data-driven decision-making process. With these takeaways in mind, you can now create impactful and informative stacked bar charts using Matplotlib.