Page 163 - Touhpad Ai
P. 163
For example:
Weight (in kg) Frequency (Number of students)
20-25 4
26-30 10
31-35 8
36-40 4
Program 3: Create a Histogram for number of hours students spent studying per week using Python Number:
[5, 7, 8, 10, 12, 14, 15, 16, 18, 20, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
This example takes number of hours per week that students spend in studying. To create a histogram from the given
data, we must first organise it into intervals. These intervals are frequently referred to as logical ranges or bins.
Code:
import seaborn as sns
import matplotlib.pyplot as plt
# List of hours students spend studying per week
study_hours = [5, 7, 8, 10, 12, 14, 15, 16, 18, 20, 9, 11, 13, 15, 17, 19, 21, 23,
25, 27,6, 8, 10, 12, 14, 16, 18, 20, 22, 24]
# Create histogram
sns.histplot(study_hours, bins=6, color='skyblue')
# Add titles and labels
plt.title("Histogram of Study Hours per Week")
plt.xlabel("Hours Spent Studying")
plt.ylabel("Number of Students")
# Display the plot
plt.show()
Data Visualization 161

