Python | Stack Plot

Python | Stack Plot: In this article, we are going to learn about the stack plot and its Python implementation, how to create stack plots using Python?
Submitted by Anuj Singh, on July 15, 2020

Stack Plots are generated by plotting different datasets vertically on top of one another rather than overlapping with one another. Matplotlib has a defined function for creating stack plot matplotlib.pyplot.stackplot(). It is often used to visualize data in the form of summation.

Following is an example showing a stack plot using matplotlib.pyplot.

Syntax:

plt.stackplot(x, y1, y2, y3, labels=labels)
plt.show()
Python | Stack Plot (1)
matplotlib.pyplot.stackplot(x, y1, y2, y3, labels=labels, colors=['b','g','y'])
Python | Stack Plot (2)
matplotlib.pyplot.stackplot(x, y1, y2, y3, labels=labels, color='y')
Python | Stack Plot (3)

Python code for stack plot

import numpy as np
import matplotlib.pyplot as plt

x = [1, 2, 3, 4, 5]
y1 = [1, 3, 1, 2, 4]
y2 = [0, 4, 4, 5, 1]
y3 = [1, 3, 8, 3, 1]

y = np.vstack([y1, y2, y3])

labels = ["F1 ", "F2", "F3"]

plt.figure()
plt.stackplot(x, y1, y2, y3, labels=labels)
plt.legend(loc='upper left')
plt.xlabel('Number Progression')
plt.ylabel('Stack')
plt.title('Stack Plot Example')
plt.show()


plt.figure()
plt.stackplot(x, y1, y2, y3, labels=labels, colors=['b','g','y'])
plt.legend(loc='upper left')
plt.xlabel('Number Progression')
plt.ylabel('Stack')
plt.title('Stack Plot Colour choice')
plt.show()

plt.figure()
plt.stackplot(x, y1, y2, y3, labels=labels, color='y')
plt.legend(loc='upper left')
plt.xlabel('Number Progression')
plt.ylabel('Stack')
plt.title('Stack Plot One Colour')
plt.show()

Output:

Output is as figure



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.