Python Create a stacked bar using matplotlib.pyplot

Compose a program to plot a stacked visual diagram with two qualities for examination, utilizing various hues utilizing matplotlib.pyplot library.

Issue articulation: Write a program in python (utilizing matplotlib.pyplot) to make a disperse plot.

Utilization of dispersing plot: Scatter plots are normally used to think about two factors (three on the off chance that you are plotting in 3 measurements), searching for connection or gatherings.

Program:

import matplotlib.pyplot as plt
import numpy as np

N=6

y1=[3,9,11,2,6,4]

y2=[6,4,7,8,3,4]

xvalues = np.arange(N)

plt.bar(xvalues,y1,color='b', label ='Team1')
plt.bar(xvalues,y2, color='r', bottom =y1, label = 'Team2')
plt.xticks(xvalues, ('V1', 'V2', 'V3', 'V4', 'V5'))

plt.xlabel('Teams')
plt.ylabel('Scores')
plt.title('Stacked Bar Graphs')
plt.legend()

Output:

Python Create a stacked bar using matplotlib.pyplot

Clarification:

We utilize the information esteems y2 above y1. y1 and y2 are two irregular factors taken for the plot. For this, we utilize the parameter base which shows which worth will be at the base. N is likewise a variable to relegate the no of bars existing in the plot.

Likewise, we utilize the organized method of numpy to uniformly space the bars and allocate its qualities to the x esteems variable. Xticks capacities give the name to the equally dispersed xvalues.

To name the tomahawks xlabel and ylabel capacities are utilized and to give the title to the plot the title work is utilized. To show the legend work is utilized lastly to show the plot the show work.

Leave a Comment

error: Alert: Content is protected!!