Program 7
7.Write a Python program to plot bar graph using labels of languages ‘C', 'C++',
'Java', 'Python', 'PHP' and students’ marks scored in above languages.
matplotlib.pyplot is a collection of command style functions that make matplotlib work like
MATLAB. Each pyplot function makes some change to a figure: e.g., creates a figure, creates a
plotting area in a figure, plots some lines in a plotting area, decorates the plot with labels, etc.
In matplotlib.pyplot various states are preserved across function calls, so that it keeps track of
things like the current figure and plotting area, and the plotting functions are directed to the
current axes.
A bar plot or bar chart is a graph that represents the category of data with rectangular bars
with lengths and heights that is proportional to the values which they represent. The bar plots
can be plotted horizontally or vertically. A bar chart describes the comparisons between the
discrete categories. One of the axis of the plot represents the specific categories being
compared, while the other axis represents the measured values corresponding to those
categories.
Creating a bar plot
The matplotlib API in Python provides the bar() function which can be used in MATLAB
style use or as an object-oriented API. The syntax of the bar() function to be used with the axes
is as follows:-
plt.bar(x, height, width, bottom, align)
The function creates a bar plot bounded with a rectangle depending on the given parameters.
Following is a simple example of the bar plot, which represents the number of students
enrolled in different courses of an institute
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
ax.bar(langs,students)
plt.show()
OUTPUT

Comments
Post a Comment