Page 123 - Informatics_Practices_Fliipbook_Class12
P. 123
Bar graphs, often known as bar charts, are a common method for displaying categorical data. Matplotlib method
plt.bar(), can be used for plotting bar graphs which accept as input parameters a list of categories that appear
on the x-axis, and the associated counts that appear on the y-axis
C T 04 1. Modify the above code to redraw the above graph using six different colors. As shown above,
we use the keyword argument color to function plt.bar() for speciying different color
encoding for different bars.
2. Consider the following two lists depicting sales data of four different products:
>>> products = ['Product A', 'Product B', 'Product C', 'Product D']
>>> sales = [350, 480, 240, 520]
Draw the bargraph visualizing the same.
3.3 Histogram
Histograms are popular data visualisation tools that offer a graphical depiction of a dataset's distribution. They are
especially useful for investigating the underlying frequency or count of various values or ranges in a dataset. The
Matplotlib module in Python includes function plt.hist() for producing and customising histograms.
3.3.1 CGPA of students
Suppose, we have CGPA of 10 students and we wish to depict pictorically the distribution of the CGPA values of
students within different ranges. In the following program, we first prompt the user to provide list of numerical values
representing the CGPA (Cumulative Grade Point Average) of students as an input. Thereafter, we plot the histogram
representing frequency distribution of CGPA obtained by students using plt.hist() function. The plt.hist()
function takes two main arguments: the data to be plotted and the bins specification. The bins define the intervals or
ranges into which the data will be divided. In our case, we specify the bins as [0, 2, 4, 6, 8, 10], which means the data
will be grouped into the following intervals: [0, 2), [2, 4), [4, 6), [6, 8), and [8, 10]. The color of the bars in the histogram
is set to red ("r") for better visibility as shown below (Fig 3.21):
>>> import matplotlib.pyplot as plt
>>> cgpa = eval(input('Enter CGPA data to be plotted as histogram: '))
Enter data to be plotted as histogram: [2,4,4,4.5,6,6.7,8,8.5,9,9.5,9.9,9]
>>> plt.hist(cgpa, bins = [0, 2, 4, 6, 8, 10], color="r")
>>> plt.xlabel('CGPA Range')
>>> plt.ylabel('Frequency')
>>> plt.title('Frequency Distribution of CGPA of students')
>>> plt.grid()
>>> plt.savefig('CGPAHistogram.png',dpi=900,bbox_inches='tight')
>>> plt.show()
Histograms are popular data visualisation tools that offer a graphical depiction of a dataset's distribution. Python
includes function plt.hist() for producing and customising histograms.
C T 05 Write a program that takes a list of grocery items purchased as an input from the user. The
program should plot the frequency distribution of grocery items purchased using a histogram.
Data Visualization 109

