Page 122 - Informatics_Practices_Fliipbook_Class12
P. 122
For this purpose, we use the function plt.bar(), which accepts as input parameters a list of categories (sport
names) that appear on the x-axis, and the associated counts that appear on the y-axis, as shown below (Fig 3.19):
>>> import matplotlib.pyplot as plt
>>> data = [60, 30, 25, 85]
>>> labels = ['Hockey', 'Volley Ball', 'Badminton', 'Cricket']
>>> #Another applications: films at boxoffice
>>> plt.bar(labels, data)
>>> plt.title('Popularity of Sports')
>>> plt.ylabel('Count of Players')
>>> plt.savefig("popularityOfSports.png")
>>> plt.show()
Fig 3.19: Bargraph showing the popularity of different sports
Note that the length of the bars is proportional to the values they represent. Thus, it is visually clear that Cricket is the
favourite sports among people as it is evident from the longest bar for cricket. Similarly, in the following example, we
will depict the popularity of different programming languages among students amongst senior secondary students of
a school. But will make one change: we will use different colors for different bars (Fig 3.20). We will use the colors red,
green, and blue for the first three bars and then repeat the colors, by specifying: color=['r', 'g', 'b']*2.
>>> import matplotlib.pyplot as plt
>>> programming_language = ['Java', 'Python', 'PHP', 'JavaScript', 'C#', 'C++']
>>> popularity = [222, 176, 88, 80, 77, 67]
>>> plt.bar(programming_language, popularity, color=['r', 'g', 'b']*2)
>>> plt.xlabel("Programming Languages")
>>> plt.ylabel("Count of Programmers")
>>> plt.title("Popularity of Programming Languages")
>>> plt.show()
Fig 3.20: Bargraph showing the popularity of different sports
108 Touchpad Informatics Practices-XII

