0% found this document useful (0 votes)
9 views

Class 3 Data Visualization in Python using Bar Chart

The document provides a comprehensive guide on creating bar charts using the PyPlot library in Python. It covers the syntax for plotting bar charts, customizing bar widths and colors, and creating multiple bar charts, along with examples for each case. Additionally, it explains how to create horizontal bar charts using the barh() function.

Uploaded by

Sanjay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Class 3 Data Visualization in Python using Bar Chart

The document provides a comprehensive guide on creating bar charts using the PyPlot library in Python. It covers the syntax for plotting bar charts, customizing bar widths and colors, and creating multiple bar charts, along with examples for each case. Additionally, it explains how to create horizontal bar charts using the barh() function.

Uploaded by

Sanjay Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Creating Bar Chart

A Bar Graph or a Bar Chart is a graphical representation of data using bars of


different heights. It can be drawn vertically or horizontally using rectangles or bars
of different heights/widths.

bar( ) of PyPlot function is used to create a bar chart where we can specify the
sequences for x-axis and corresponding sequence to be placed on y-axis. Each y
value is plotted as bar on corresponding x-value on axis.
To create the bar chart first import matplotlib.pyplot.
Syntax is:
import matplotlib.pyplot as pl
For example:
import matplotlib.pyplot as pl
Cities=['Delhi','Mumbai','Kolkata','Ahmedabad',
'Surat']
Population=[212345,543216,654321,787654,3
87654]
pl.bar(Cities,Population)
pl.xlabel('Cities')
pl.ylabel('Population')
pl.show()

For example: Consider the Medal tally of Commonwealth games 2018 given
below:
Country Gold Silver Bronze Total
Australia 80 59 59 198
England 45 45 46 136
India 26 20 20 66
Canada 15 40 27 82
New 15 16 15 46
Zealand
South Africa 13 11 13 37
Wales 10 12 14 36
Scotland 9 13 22 44
Nigeria 9 9 6 24
Cyprus 8 1 5 14
Q1- Consider the reference table given above. Write the program to plot a bar
chart from the medals won by Australia.
import matplotlib.pyplot as pl
Info=
['Gold','Silver','Bronze','Total']
Australia= [80, 59, 59, 198]
pl.bar(Info, Australia, color='r')
pl.xlabel("Medal type")
pl.ylabel("Australia Medal count")
pl.show( )

Q2- Consider the reference table given above. Write the program to plot a bar
chart from the medals won by Australia. In the same chart, plot medals won by
India too.
import matplotlib.pyplot as pl
Info= ['Gold','Silver','Bronze','Total']
Australia= [80, 59, 59, 198]
India=[26,20,25,66]
pl.bar(Info, Australia, color='r')
pl.bar(Info, India, color='b')
pl.xlabel("Medal type")
pl.ylabel("Australia and India Medal
count")
pl.show( )

Customising the Bar Chart:


By default, bar chart draws bars with equal widths and having a default width of
0.8 units on a bar chart. But you can change the widths of the bars.
 You can specify a different width(other than the default width) for all the
bars of a bar chart.
 You can specify different widths for different bars of a bar chart.
(i) To specify common width (other than the default width) for all bars, you
can specify width argument having a scalar float value in the bar() function
i.e. as:
Syntax is:
<matplotlib.pyplot>.bar(<x-sequence>,<y-sequence>, width=<float value>)
For example:
Bar with default Bar with ½ width Bar with 0.3 width
width

pl.bar(Cities,Popu pl.bar(Cities,Population,w pl.bar(Cities,Population,


lation) idth=1/2) width=0.3)

(ii) To specify different widths for different bar chart you can specify width
argument having a sequence (such as list or tuples) containing widths for
each of the bars, in the bar function.
Syntax is:
<matplotlib.pyplot>.bar(<x-sequence>,<y-sequence>, width=<width value
sequence with float value>)
For example:
import matplotlib.pyplot as pl
Cities=['Delhi','Mumbai','Kolkata','Ahmedabad','Surat']
Population=[212345,543216,654321,787654,387654]
pl.bar(Cities,Population,width=[.3,0.4,.5,.25,.7])
pl.xlabel('Cities')
pl.ylabel('Population')
pl.show()

Note: While specifying width care should be made to specify width for all
the bars. Otherwise python generates error.
Changing Colors of the Bar in a Bar Chart:
By default, a bar chart draws bars with same default color. But you can
always change the color of the bars.
 You can specify a different color for all the bars of a bar chart.
 You can specify a different color for different bars of a bar chart.
(i) To specify common color (other than the default color) for all bars, you
can specify color argument having a valid color code/name in the bar()
function.
Syntax is:
<matplotlib.pyplot>.bar(<x-sequence>,<y-sequence>, color=<color code/name>)
The color given will apply to all the bars in the chart.
For example:
import matplotlib.pyplot as pl
Cities=['Delhi','Mumbai','Kolkata','Ahmedabad','Surat'
]
Population=[212345,543216,654321,787654,387654]
pl.bar(Cities,Population,color='r')
pl.xlabel('Cities')
pl.ylabel('Population')
pl.show()

Here all the bars in the same color.


(ii) To specify different color for different bars of a bar chart, you can specify
color argument as sequence of color code/name in the bar() function.
Syntax is:
<matplotlib.pyplot>.bar(<x-sequence>,<y-sequence>, color=[<color code/name sequence>])
For example:
import matplotlib.pyplot as pl
Cities=['Delhi','Mumbai','Kolkata','Ahmedabad','Surat']
Population=[212345,543216,654321,787654,387654
]
pl.bar(Cities,Population,color=['r','green','b','y','k'])
pl.xlabel('Cities')
pl.ylabel('Population')
pl.show()

Q3 Consider the reference table given above. Write a program to plot a bar
chart from the medals won by India. Make sure that the Gold, Silver, Bronze and
Total tally is represented through different colors.
import matplotlib.pyplot as pl
Info= ['Gold','Silver','Bronze','Total']
India=[26,20,25,66]
pl.bar(Info, India, color=['r','b','c','yellow'])
pl.xlabel("Medal type")
pl.ylabel("India Medal count")
pl.show( )

Creating Multiple Bar Chart:


Often in real life, you may need to plot multiple data ranges on the same bar chart
creating multiple bars. PyPlot does not provide a specific functions for creating
multiple bar chart.
Steps to Creating Multiple Bar Chart:
(i) Decide number of X points- Firstly, you need to determine how many X
points you will need. For this calculate the number of entries in the ranges
being plotted. Suppose you are plotting two sequences A and B, so the
length of sequences A and B being plotted will determine the number of X
points in our case. A sequences can be created using range or
numpy.arange() functions. The shape of both the sequences must have
same shape i.e. number of elements)
(ii) Decide thickness of each bar and accordingly adjust X points on X-axis. If
you want to plot two bars per X point, then carefully think of the thickness
of each bar. Say you want the thickness of each bar as .3, then for first
range keep it as X and for the second data range, make the X point as
X+.3.
(iii) Give different colours to different data ranges using color argument of
bar( ) function.
(iv) The width argument remains the same for all ranges being plotted.
(v) Plot using bar() for each range separately, i.e. the number of bars decide
the number of bar( ) functions you will be using to plot different bars on
same plot.
For example: To plot ranges A=[2,4,6,8], B=[2.8,3.5,6.5,7.7], C= C=[3,2,4.3,6.5]
import numpy as np
import matplotlib.pyplot as pl
A=[2,4,6,8]
B=[2.8,3.5,6.5,7.7]
C=[3,2,4.3,6.5]
X=np.arange(4)
pl.bar(X+0.0,A,color='r',width=.25)
pl.bar(X+0.25,B,color='b',width=.25)
pl.bar(X+0.50,C,color='g',width=.25)
pl.show()

Creating a Horizontal Bar Chart:


barh() function is used to create horizontal bar. The x axis in bar() will become y
axis label in barh( ).
For example:
import matplotlib.pyplot as pl
Cities=['Delhi','Mumbai','Kolkata','Ahmedabad','Surat']
Population=[212345,543216,654321,787654,387654]
pl.bar(Cities,Population)
pl.xlabel('Cities')
pl.ylabel('Population')
pl.show()
import matplotlib.pyplot as pl
Cities=['Delhi','Mumbai','Kolkata','Ahmedabad','Surat']
Population=[212345,543216,654321,787654,387654]
pl.barh(Cities,Population)
pl.xlabel('Cities')
pl.ylabel('Population')
pl.show()

You might also like