Python | Grouped Bar Chart

Python | Grouped Bar Chart: Here, we will learn about the grouped bar chart and its Python implementation.
Submitted by Anuj Singh, on July 14, 2020

Grouped bar charts are very easy to visualize the comparison between two similar quantities such as marks comparison between two students. It is an extension of a simple bar graph and in this article, we are going to illustrate an example in which we plot marks of five different students from the same class in Mathematics and Science. Furthermore, we are going to change the color and width of the bars for a clear understanding of the usage of the Grouped Bar Chart.

Syntax:

plt.bar(x - width/2, math_means, width, label='Mathematics')
plt.bar(x + width/2, science_means, width, label='Science')
Python | Grouped Bar Chart (1)
plt.bar(x - width/2, math_means, width, label='Mathematics', color='y')
plt.bar(x + width/2, science_means, width, label='Science', color='purple')
Python | Grouped Bar Chart (2)
plt.bar(x - width/2, math_means, 0.8*width, label='Mathematics', color='y')
plt.bar(x + width/2, science_means, 0.8*width, label='Science', color='purple')
Python | Grouped Bar Chart (3)

Python code for grouped bar chart

import matplotlib.pyplot as plt
import numpy as np

labels = ['Tom', 'Dick', 'Harry', 'Sally', 'Sue']
math_means = [20, 34, 30, 35, 27]
science_means = [25, 32, 34, 20, 25]

x = np.arange(len(labels))  # the label locations
width = 0.35  # the width of the bars

# Figure 1
plt.figure()
plt.bar(x - width/2, math_means, width, label='Mathematics')
plt.bar(x + width/2, science_means, width, label='Science')

# Add some text for labels, title and 
# custom x-axis tick labels, etc.
plt.ylabel('Scores')
plt.title('Scores by group')
plt.xticks( np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
plt.legend()
plt.show()

# Figure 2
plt.figure()
plt.bar(x - width/2, math_means, width, label='Mathematics', color='y')
plt.bar(x + width/2, science_means, width, label='Science', color='purple')

# Add some text for labels, title and 
# custom x-axis tick labels, etc.
plt.ylabel('Scores')
plt.title('Scores by group')
plt.xticks( np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
plt.legend()
plt.show()

# Figure 2
plt.figure()
plt.bar(x - width/2, math_means, 0.8*width, label='Mathematics', color='y')
plt.bar(x + width/2, science_means, 0.8*width, label='Science', color='purple')

# Add some text for labels, title and 
# custom x-axis tick labels, etc.
plt.ylabel('Scores')
plt.title('Scores by group')
plt.xticks( np.arange(5), ('Tom', 'Dick', 'Harry', 'Sally', 'Sue') )
plt.legend()
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.