Page 465 - AI Ver 3.0 class 10_Flipbook
P. 465
Plotting a Histogram chart
It represents the frequency of the variable at different points of time. It is used for continuous data and the
bars created show no gap in between. X-axis represents the bin ranges while Y-axis gives information about
frequency.
A histogram displays the numerical data grouped into bars also known as "bins" of equal size. The height of the
bar represents the data point. Histogram helps you find the number of bars, the range of numbers that go into
each bar, and the labels for the bar edges.
Before you go for designing a Histogram you need to finalise the width of each bin. If we go from 10 to 250
using bins with a width of 50 then the data can easily adjust in 5 bins. IT is always a good practice of using a
decent number of bins in the designing of a histogram.
[1]: import matplotlib.pyplot as plt
# Number of schools in each city
schools = [10,25,29]
# Creating the histogram with bins
plt.hist(schools, bins=[10,15, 20, 25, 30])
# Adding labels and title
plt.xlabel('Number of Schools')
plt.ylabel('Frequency')
plt.title('Distribution of Schools in Cities')
# Customizing x-axis tick labels
plt.xticks([15, 20, 25, 30])
# Displaying the histogram
plt.show()
Let's draw a histogram to represent marks out of 100 of 40 students in class.
Matplotlib uses a built-in function - hist () to create a histogram.
For example,
[1]: from matplotlib import pyplot as plt
import numpy as np
fig,ax = plt.subplots(1,1)
marks = np.array([19,16,28,45,37,34,13,49,42,44])
ax.hist(marks, bins = [10,20,30,40,50])
ax.set_title("histogram of result")
ax.set_xlabel('marks')
ax.set_ylabel('no. of students')
plt.show()
Advance Python (Practical) 463

