Page 327 - Informatics_Practices_Fliipbook_Class12
P. 327
Program 19: Consider the following dataset comprising grades and the count of students who has scored these grades
in a class of 50 students in a biology exam:
biologyGrades = ["A", "B", "C", "D", "F"]
gradesCount = [10, 15, 12, 8, 5]
Write a Python program to create a bar graph comparing distribution of grades using distinct colours. Label the axes
and provide a title. Save the figure as "gradesDistribution.png."
Ans. import matplotlib.pyplot as plt
biologyGrades = ["A", "B", "C", "D", "F"]
gradesCount = [10, 15, 12, 8, 5]
colors = ['green', 'blue', 'orange', 'purple', 'red']
plt.bar(biologyGrades, gradesCount, color=colors)
plt.xlabel('Grades')
plt.ylabel('Number of Students')
plt.title('Distribution of Grades in Biology Exam')
plt.savefig('gradesDistribution.png')
plt.show()
Program 20: Consider the following unemployment rates of two different regions:
regionA = [5.2, 4.8, 5.1, 6.0, 4.9]
regionB = [6.3, 6.0, 6.5, 7.2, 7.1]
Write a Python program to draw a single graph with two line plots, one for Region A and another for Region B, using
distinct colors and styles. Label the axes and provide a title. Save the figure as "unemploymentComparison.png."
Ans. import matplotlib.pyplot as plt
regionA = [5.2, 4.8, 5.1, 6.0, 4.9]
regionB = [6.3, 6.0, 6.5, 7.2, 7.1]
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May']
plt.plot(months, regionA, label='Region A', color='blue', linestyle='-', marker='o')
plt.plot(months, regionB, label='Region B', color='red', linestyle='--', marker='s')
plt.xlabel('Month')
plt.ylabel('Unemployment Rate (%)')
plt.title('Unemployment Comparison between Region A and Region B')
plt.legend()
plt.savefig('unemploymentComparison.png')
plt.show()
Practical 313

