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

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.