Table of Contents

Stack Plots Example – Matplotlib Tutorial

Let’s take an example to understand Stack Plot. Consider a scenario where a person prefers to watch and enjoy Youtube, Netflix, Amazon Prime, HBO Now, Hulu over a week. So, when we create a stack plot, we will be able to see the pattern of watching different video streaming apps in a span of 7 days from Monday to Sunday as a whole by the person.

 

Creating a Stack Plot using matplotlib

We will simply take one list for x coordinates and five lists for our y coordinates. The new function that we will introduce here is stackplots. Rest of the functions we have already discussed such as xlabel() ylabel() title() legend() show()

The below python code is a basic example of how to create and draw a stack plot using Matplotlib.

import matplotlib.pyplot as plt

Days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" ]
Youtube = [3, 3.5, 4, 2, 1, 5, 6]
Netflix = [1, 3, 2, 1, 2, 4, 5]
Amazon_Prime = [0.8, 0.8, 0.8, 0.8 ,0.8, 3, 3 ]
HBO_Now = [0.6, 0.6, 0.6, 0.6, 0.6, 2, 2]
Hulu = [0.3, 0.3, 0.3, 0.3, 0.3, 1, 1]
labels =["Youtube", "Netflix", "Amazon_Prime", "HBO_Now", "Hulu"]
plt.stackplot(Days, Youtube, Netflix, Amazon_Prime, HBO_Now, Hulu, labels=labels)
plt.xlabel("Days" )
plt.ylabel("Video Streaming Apps")
plt.title("Stack Plot Example")

plt.legend()
plt.show()

 

 

Given below is the code shown in Python Online code editor where you can edit the code to experiment with the data and stack plot.