 # Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4

• 152 Views Matplotlib Tutorial in Python

*Matplotlib Stack Plots/Bars | Chapter 4

In this chapter we will learn to create Matplotlib/Python Stack Plots/Bars.Stack Plots are also called area graphs/plots in general.

Matplotlib Tutorials in Python - Creating a Simple Stack Plot in Matplotlib

A good example of using a stack plot, will be to plot the runs scored by both the batsmen ath the end of each over of a match in Cricket. We will use plt.stackplot() to draw the plot of the runs scored by each batsman.

``````# matplotlib_stack_bar.py

import matplotlib.pyplot as plt

plt.style.use('fivethirtyeight')

overs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

batsman1 = [4, 10, 12, 14, 14, 18, 22, 30, 40, 48]

batsman2 = [0, 6, 12, 18, 26, 28, 32, 33, 34, 36]

plt.stackplot(overs, batsman1, batsman2)

plt.title('Total runs scored')
plt.tight_layout()
plt.show()

`````` Matplotlib Tutorials in Python - Customizing the Stack Plot in Matplotlib

We can customize the Matplotlib Stack Plot by adding labels, colors etc.

To add the labels, we will create a list of labels and pass it to plt.stackplot() and then call the plt.legend() method.

``````# matplotlib_stack_bar.py

import matplotlib.pyplot as plt

plt.style.use('fivethirtyeight')

overs = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
batsman1 = [4, 10, 12, 14, 14, 18, 22, 30, 40, 48]
batsman2 = [0, 6, 12, 18, 26, 28, 32, 33, 34, 36]

labels = ['Shikhar', 'Rohit']
plt.stackplot(overs, batsman1, batsman2, labels=labels)
plt.legend()
plt.title('Total runs scored')
plt.tight_layout()
plt.show()
`````` Here, the legends are overlapping the plot, you can pass loc to plt.legend() to hard-code the location of the legends. And you can add colors by passing a list of colors to the plt.stackplot().

``````colors = ['blue', 'green']
plt.stackplot(overs, batsman1, batsman2, labels=labels, colors=colors)
plt.legend(loc = 'upper left')
``````

You can either add numerical coordinates or the following values to loc:-

``````    best
upper right
upper left
lower left
lower right
right
center left
center right
lower center
upper center
center
`````` ## Video Tutorial

Matplotlib Tutorial in Python | Chapter 1 | Introduction

Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

Pie Charts in Python | Matplotlib Tutorial in Python | Chapter 3

Matplotlib Stack Plots/Bars | Matplotlib Tutorial in Python | Chapter 4

Filling Area on Line Plots | Matplotlib Tutorial in Python | Chapter 5

Python Histograms | Matplotlib Tutorial in Python | Chapter 6

Scatter Plotting in Python | Matplotlib Tutorial | Chapter 7

Plot Time Series in Python | Matplotlib Tutorial | Chapter 8

Python Realtime Plotting | Matplotlib Tutorial | Chapter 9

Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10

Python Candlestick Chart | Matplotlib Tutorial | Chapter 11

In case of any query, you can leave the comment below.

If you want to support our work. You can do it using Patreon.

In the next chapter we will learn about Filing Area on Line Plots in Matplotlib.

By Udit Vashisht

## Matplotlib Tutorial in Python

In this series of Matplotlib Tutorials in Python, we will cover all the concepts from beginners to expert level. Starting with how to install Matplotlib library to how to create the plots, this series is an exhaustive tutorial and by the end of this series you ...

By Udit Vashisht

# Introduction to Python

Curiosity and zeal to learn something new have probably brought you here. It doesn’t matter whether you are new to programming or switching from some other redundant or less useful language, you must be having a few very important questions in your mind. We will be answering ...