0% found this document useful (0 votes)
27 views3 pages

Experiment No.2 (DV)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Experiment No.2 (DV)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMET NO.

(2)
AIM: IMPLIMENTING A BAR CHARTS USING Matplotlib.

THEORY
A bar chart or bar graph is a chart or graph that presents categorical data with rectangular
bars with heights or lengths proportional to the values that they represent. The bars can be
plotted vertically or horizontally.
A bar graph shows comparisons among discrete categories. One axis of the chart shows the
specific categories being compared, and the other axis represents a measured value.
Matplotlib API provides the bar() function that can be used in the MATLAB style use as
well as object oriented API. The signature of bar() function to be used with axes object is as
follows –
SYNTAX:
ax.bar(x, height, width, bottom, align)

The function makes a bar plot with the bound rectangle of size (x −width = 2; x +
width=2; bottom; bottom + height).
The parameters to the function are −

x sequence of scalars representing the x coordinates of the bars. align controls if x


is the bar center (default) or left edge.

height scalar or sequence of scalars representing the height(s) of the bars.

width scalar or array-like, optional. the width(s) of the bars default 0.8

bottom scalar or array-like, optional. the y coordinate(s) of the bars default None.

align {‘center’, ‘edge’}, optional, default ‘center’


The function returns a Matplotlib container object with all bars.

CODE_1:
Import matplotlib.pyplot as plt
left=[1,2,3,4,5]
height=[10,24,36,40,5]
tick_label=['one','two','three','four','five']
plt.bar(left,height,tick_label=tick_label,width=0.8,color=['red','green'])
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.title('my bar chart!')
plt.show()

O/P: bar chart

• Here, we use plt.bar() function to plot a bar chart.


• x-coordinates of the left side of bars are passed along with the heights
of bars.
• you can also give some names to x-axis coordinates by defining
tick_labels

CODE-2: Following is a simple example of the Matplotlib bar plot. It shows the number of
students enrolled for various courses offered at 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()
O/P:

RESULT: THE ABOVE PROGRAMS ARE SUCCESSFULLY EXECUTED AND NOW


LEARNED THE USE AND ABILITY TO APPLY MATPLOTLIB IN BAR CHART
PROGRAMMING.

You might also like