- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to display stacked bar chart using matplotlib in Python?
Matplotlib is a popular Python package that is used for data visualization. Visualizing data is a key step since it helps understand what is going on in the data without actually looking at the numbers and performing complicated computations. It helps in communicating the quantitative insights to the audience effectively.
Matplotlib is used to create 2 dimensional plots with the data. It comes with an object oriented API that helps in embedding the plots in Python applications. Matplotlib can be used with IPython shells, Jupyter notebook, Spyder IDE and so on.
It is written in Python. It is created using Numpy, which is the Numerical Python package in Python.
Python can be installed on Windows using the below command −
pip install matplotlib
The dependencies of Matplotlib are −
Python ( greater than or equal to version 3.4) NumPy Setuptools Pyparsing Libpng Pytz Free type Six Cycler Dateutil
Stacked bar plots show the data points of two values in a single rectangular box.
Let us understand how Matplotlib can be used to create a stacked plot −
import matplotlib.pyplot as plt labels = ['A1', 'A2', 'A3', 'A4'] val_1 = [34, 56, 78, 91] val_2 = [20, 56, 32, 89] val_3 = [1, 3, 5, 3] val_4 = [3, 5, 3, 4] width = 0.40 fig, ax = plt.subplots() ax.bar(labels, val_1, width, yerr=val_3, label='Label_1') ax.bar(labels, val_2, width, yerr=val_4, bottom=val_1, label='Label_2') ax.set_ylabel('Y−axis') ax.set_title('X−axis') ax.legend() plt.show()
The required packages are imported and its alias is defined for ease of use.
The labels for the stacked chart and values for the labels/bars are defined.
An empty figure is created using the ‘figure’ function.
The ‘subplot’ function is used to create an area to plot the graph.
The data is plotted using the ‘plot’ function.
The set_xlabel, set_ylabel and set_title functions are used to provide labels for ‘X’ axis, ‘Y’ axis and title.
The plot is defined as a bar chart by specifying ‘bar’.
It is shown on the console using the ‘show’ function.
- Horizontal stacked bar chart in Matplotlib
- How to Create a Diverging Stacked Bar Chart in Matplotlib?
- How to create a stacked bar chart using JavaFX?
- How to create stacked bar chart using ggvis in R?
- How to create a stacked bar chart for my DataFrame using Seaborn in Matplotlib?
- How to create horizontal stacked bar chart using ggvis in R?
- Python Pandas - Plot a Stacked Horizontal Bar Chart
- How to display percentage above a bar chart in Matplotlib?
- How to create a 100% stacked Area Chart with Matplotlib?
- Create stacked bar chart with percentages on Y-axis using ggplot2 in R.
- How can I display text over columns in a bar chart in Matplotlib?
- How to create a stacked area chart using JavaFX?
- How to plot a very simple bar chart (Python, Matplotlib) using input *.txt file?
- How to plot a bar chart for a list in Python matplotlib?
- How to change Bar Chart values to percentages in Matplotlib?