ByteBoard Notes Notes

Given school result data, analyse subject-wise performance and plot it

Informatics Practices XII · Experiment 18 of 38 · Pandas

★ CBSE suggested12-IPMatplotlib programs
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

  1. Store the subjects and their class averages in two lists.
  2. Draw a bar chart with plt.bar().
  3. Add x and y labels and a title.
  4. 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)
Figure 1
Bar chart of Class XII average marks by subject: English 72, Maths 65, Physics 70, Chemistry 68, IP 84; IP is highest and Maths lowest.

✓ 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

  1. Draw the bars horizontally with plt.barh(). (Not in the syllabus; for curiosity only.)
  2. Give each bar a different colour using a list of colours.
  3. 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.
Back to the 12-IP index