Class 3 Data Visualization in Python using Bar Chart
Class 3 Data Visualization in Python using Bar Chart
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( )
(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()
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( )