Given school result data, analyse subject-wise performance and plot it
Expt. No.18
Page No.36
Subject-wise result bar chart
Aim
To write a Python program that plots the subject-wise average marks of a class as a bar chart with axis labels and a title.
Concepts used
Algorithm
- Store the subjects and their class averages in two lists.
- Draw a bar chart with plt.bar().
- Add x and y labels and a title.
- Show the chart.
Program
# Subject-wise performance as a bar chart import matplotlib.pyplot as plt subjects = ["English", "Maths", "Physics", "Chemistry", "IP"]average = [72, 65, 70, 68, 84] plt.bar(subjects, average, color="teal")plt.xlabel("Subject")plt.ylabel("Average marks")plt.title("Class XII: subject-wise average marks")plt.show()
A marked line continues the line above it. In your file, write it on the same line.
Output
$ python plot_subject_wise_bar.py
(no printed output)
✓ Printed by running this program, not typed by hand.
Result
The bar chart shows one bar per subject; IP (84) has the tallest bar and Maths (65) the shortest, so the class did best in IP.
Viva voce: what the examiner may ask
01Which function draws a bar chart?
plt.bar(x, height).
02What do xlabel() and ylabel() do?
They add a label under the x-axis and beside the y-axis.
03When is a bar chart better than a line chart?
For comparing separate categories, such as subjects; a line chart suits values that change over time.
Variations for the lab test
- Draw the bars horizontally with
plt.barh(). (Not in the syllabus; for curiosity only.) - Give each bar a different colour using a list of colours.
- Save the chart as subjects.png with
plt.savefig().
! Common mistake Calling
plt.show() before plt.title(): the title is added to a new, empty figure and is never seen.