Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Stacked Percentage Bar Plot In MatPlotLib
Next article icon

In this article, we will learn how to Create a stacked bar plot inMatplotlib. Let's discuss some concepts:

  • Matplotlib is a tremendous visualization library in Python for 2D plots of arrays. Matplotlib may be a multi-platform data visualization library built onNumPyarrays and designed to figure with the broader SciPy stack.
  • A bar plot or bar graph may be a graph that represents the category of knowledge with rectangular bars with lengths and heights that's proportional to the values which they represent. The bar plots are often plotted horizontally or vertically.
  • Stacked bar plots represent different groups on the highest of 1 another. The peak of the bar depends on the resulting height of the mixture of the results of the groups. It goes from rock bottom to the worth rather than going from zero to value.

Approach:

  1. Import Library (Matplotlib)
  2. Import / create data.
  3. Plot the bars in the stack manner.

Example 1: (Simple stacked bar plot)

Python3
# importing packageimportmatplotlib.pyplotasplt# create datax=['A','B','C','D']y1=[10,20,10,30]y2=[20,25,15,25]# plot bars in stack mannerplt.bar(x,y1,color='r')plt.bar(x,y2,bottom=y1,color='b')plt.show()

Output :

Example 2: (Stacked bar chart with more than 2 data)

Python3
# importing packageimportmatplotlib.pyplotaspltimportnumpyasnp# create datax=['A','B','C','D']y1=np.array([10,20,10,30])y2=np.array([20,25,15,25])y3=np.array([12,15,19,6])y4=np.array([10,29,13,19])# plot bars in stack mannerplt.bar(x,y1,color='r')plt.bar(x,y2,bottom=y1,color='b')plt.bar(x,y3,bottom=y1+y2,color='y')plt.bar(x,y4,bottom=y1+y2+y3,color='g')plt.xlabel("Teams")plt.ylabel("Score")plt.legend(["Round 1","Round 2","Round 3","Round 4"])plt.title("Scores by Teams in 4 Rounds")plt.show()

Output :

Example 3: (Stacked Bar chart usingdataframeplot)

Python3
# importing packageimportmatplotlib.pyplotaspltimportnumpyasnpimportpandasaspd# create datadf=pd.DataFrame([['A',10,20,10,26],['B',20,25,15,21],['C',12,15,19,6],['D',10,18,11,19]],columns=['Team','Round 1','Round 2','Round 3','Round 4'])# view dataprint(df)# plot data in stack manner of bar typedf.plot(x='Team',kind='bar',stacked=True,title='Stacked Bar Graph by dataframe')plt.show()

Output :

  Team  Round 1  Round 2  Round 3  Round 40    A       10       20       10       261    B       20       25       15       212    C       12       15       19        63    D       10       18       11       19


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp