On this page
Data visualisation sits in Unit 1 of Class 12 Informatics Practices, and it turns up in the theory paper, the practical exam and the project. The code is short, which is exactly why small slips cost marks. This post covers the three plots the syllabus names, with code tested on Matplotlib 3.9, a histogram counted by hand, and the common errors.
What does the syllabus ask for?
The CBSE Informatics Practices curriculum for 2026-27 lists, under "Data Visualization": the purpose of plotting; drawing and saving line plots, bar graphs and histograms using Matplotlib; and customising plots by adding a label, title and legend. Unit 1, which also covers Pandas, carries 25 marks. In the 30-mark practical, "Programs using Pandas and Matplotlib" carry 8 marks, and the practical file must include at least 4 Matplotlib programs. The textbook chapter is NCERT's Plotting Data using Matplotlib, Chapter 4 of the Class 12 IP book. Check the curriculum for your own exam year.
How do you draw a line plot with a legend?
NCERT imports the pyplot module as `plt`, the alias used throughout the chapter. Here are four unit test scores (sample data):
import matplotlib.pyplot as plt
tests = ["UT1", "UT2", "UT3", "UT4"]
physics = [62, 68, 71, 79]
maths = [75, 70, 78, 84]
plt.plot(tests, physics, marker="o", label="Physics")
plt.plot(tests, maths, marker="s", linestyle="--", label="Maths")
plt.title("Unit test marks")
plt.xlabel("Test")
plt.ylabel("Marks out of 100")
plt.legend()
plt.savefig("marks_line.png")
plt.show()Two lines share one set of axes, and `legend()` names them. The names come from `label=`. The Matplotlib legend documentation explains that, with no extra arguments, legend entries are taken from each plotted line's label. Leave out `label=` and the legend has nothing to show.
Why does a saved plot come out blank?
Because of the order of the last two lines. The documentation for show() says that at the end of a blocking `show()` the figure is closed, and calling `savefig()` afterwards "would save a new and thus empty figure". Always call `plt.savefig("name.png")` before `plt.show()`.
How do bar graphs and histograms differ?
A bar graph compares separate categories: one bar per test, subject or city. `plt.bar(tests, physics)` draws one. A histogram shows how numeric data is spread, by counting how many values fall into each range, called a bin.
Worked example. Fifteen students scored (sample data): 34, 45, 52, 58, 60, 61, 67, 70, 72, 75, 78, 80, 85, 91, 100.
marks = [34, 45, 52, 58, 60, 61, 67, 70, 72, 75, 78, 80, 85, 91, 100]
plt.hist(marks, bins=[0, 20, 40, 60, 80, 100], edgecolor="black")
plt.title("Distribution of marks")
plt.xlabel("Marks")
plt.ylabel("Number of students")
plt.show()The hist() documentation says every bin except the last is half-open: it includes its left edge and excludes its right edge. The last bin includes both edges. So:
0 to 20: no scores, count 0.
20 to 40: 34, count 1.
40 to 60: 45, 52, 58, count 3. The score of 60 does not go here.
60 to 80: 60, 61, 67, 70, 72, 75, 78, count 7.
80 to 100 (including 100): 80, 85, 91, 100, count 4.
The counts add to 0 + 1 + 3 + 7 + 4 = 15, one per student. Running the code returns exactly these counts. Always add your counts to check that no score was lost or counted twice.
How does Pandas plotting fit in?
NCERT also teaches plotting straight from a DataFrame with `df.plot(kind="line")`, `kind="bar"` or `kind="hist"`. With the same data:
import pandas as pd
df = pd.DataFrame({"Test": tests, "Physics": physics, "Maths": maths})
df.plot(kind="bar", x="Test", title="Unit test marks")
plt.show()Here the legend appears without `label=`, because Pandas uses the column names. If you are rusty on building DataFrames, read our guide to Pandas Series and DataFrames for Class 12 IP first.
Which mistakes lose marks?
Wrong chart for the data. Categories need a bar graph; a spread of numbers needs a histogram.
Missing title or axis labels. NCERT says a chart should always have a title, labelled axes and a legend when more than one data set is plotted.
`plt.legend()` with no labels.
`savefig()` after `show()`.
Wrong bin counts in written answers, from forgetting the half-open rule.
Mismatched list lengths. `plt.plot(x, y)` needs the same number of x and y values.
A practice routine for the practical exam
Type the three programs above from memory, run them and compare the output with this post.
Change one thing at a time (marker, colour, line style, bin edges) and predict the result before you run it.
For three different data sets, decide in one sentence whether a line, bar or histogram fits, and why.
Put four polished Matplotlib programs in your practical file, each with a title, axis labels and a saved image.
If an error message or a wrong-looking chart has you stuck, Fermi's AI tutor notices where you are stuck and decides whether to explain, ask or show, so you fix the idea and not just the line of code.
Tonight, run the histogram example and change the bins to `range(30, 110, 10)`; count the bars by hand before you look.



