Page 328 - Informatics_Practices_Fliipbook_Class12
P. 328
Program 21: Consider the following average customer ratings for 50 customer visits recorded by a restaurant:
customerRatings = [4.5, 4.8, 3.9, 4.2, 4.0, 4.3, 4.7, 3.8, 4.6, 4.1, 4.9, 3.7,
4.4, 4.2, 4.5, 4.0, 3.6, 4.8, 4.2, 4.3, 3.9, 4.5, 4.7, 4.2, 4.4, 4.6, 3.8, 4.1,
4.3, 4.0]
These ratings are on a scale of 1 to 5, where 1 is the lowest and 5 is the highest. Write a Python program using
matplotlib to create a histogram to visualize the distribution of customer ratings. Label the axes and provide a title
for the figure. Save the figure as "customerRatings.png."
Ans. import matplotlib.pyplot as plt
customerRatings = [4.5, 4.8, 3.9, 4.2, 4.0, 4.3, 4.7, 3.8, 4.6, 4.1, 4.9,
3.7, 4.4, 4.2, 4.5, 4.0, 3.6, 4.8, 4.2, 4.3, 3.9, 4.5, 4.7, 4.2, 4.4, 4.6,
3.8, 4.1, 4.3, 4.0]
plt.hist(customerRatings, bins=5, edgecolor='black', alpha=0.7)
plt.xlabel('Customer Ratings')
plt.ylabel('Number of Visits')
plt.title('Distribution of Customer Ratings')
plt.xticks([1, 2, 3, 4, 5]) # Set the x-axis ticks to correspond to the rating
scale
plt.grid(axis='y', linestyle='--', alpha=0.6)
plt.savefig('customerRatings.png')
plt.show()
Program 22: Consider the following dataset containing the scores of 30 students in a class test:
studentScores = [78, 85, 90, 72, 88, 95, 82, 79, 87, 83, 78, 92, 84, 88, 75, 80,
86, 89, 91, 94]
Write a Python program using matplotlib to create a histogram to visualize the distribution of test scores. Label the
axes and provide a title for the figure. Save the figure as "studentScores.png."
Ans. import matplotlib.pyplot as plt
studentScores = [78, 85, 90, 72, 88, 95, 82, 79, 87, 83, 78, 92, 84, 88, 75,
80, 86, 89, 91, 94]
plt.hist(studentScores, bins=10, edgecolor='black', alpha=0.7, color='lightblue')
plt.xlabel('Test Scores')
plt.ylabel('Number of Students')
plt.title('Distribution of Test Scores')
plt.savefig('studentScores.png')
plt.show()
314 Touchpad Informatics Practices-XII

