Data visualisation is the graphical representation of data to make complex information understandable at a glance. A well-chosen chart can reveal trends, comparisons, and distributions that a long table of numbers hides. In Informatic Practices, we use the matplotlib library of Python to create visualisations, importing its plotting module as matplotlib.pyplot and referring to it by the short alias plt.
The most common charts in the Class 11 syllabus are the line chart, bar chart, and pie chart. Each chart type suits a particular purpose: line charts show trends over time, bar charts compare quantities across categories, and pie charts show how a whole is divided into parts. Visualisation is not only about drawing; it is about reading data correctly. A student must know what a chart reveals, what each axis and label means, and how the plt functions work together to build a complete figure.
This chapter explains the pyplot functions needed to create and customise charts, the typical structure of a visualisation program, and how to interpret the output. We will write complete programs for line, bar, and pie charts, learn to add titles, labels, legends, and gridlines, and understand the role of functions such as plot(), bar(), pie(), show(), xlabel(), ylabel(), and legend(). We will also touch on scatter plots and histograms, which extend the ideas into two-variable and frequency analysis.
Matplotlib is an external library that must be installed using pip and then imported. The standard import is:
import matplotlib.pyplot as plt
The alias plt is used throughout the code. Inside a Jupyter notebook, the command %matplotlib inline displays charts below the code cell, while in script mode the plt.show() function displays the chart in a separate window.
A line chart plots points connected by lines, making it ideal for showing trends over a continuous range such as time.
import matplotlib.pyplot as plt
years = [2019, 2020, 2021, 2022, 2023]
students = [120, 150, 135, 170, 190]
plt.plot(years, students)
plt.title("Students Over the Years")
plt.xlabel("Years")
plt.ylabel("Number of Students")
plt.grid(True)
plt.show()
Key points:
- plt.plot(x, y) draws the line using the two lists.
- plt.title(), plt.xlabel(), plt.ylabel() label the chart and axes.
- plt.grid(True) adds gridlines for easier reading.
- plt.show() displays the figure.
We can customise the line using additional arguments such as color, marker, and linestyle:
plt.plot(years, students, color="red", marker="o", linestyle="--")
A bar chart uses rectangular bars to compare quantities across different categories. Vertical bars are drawn with plt.bar().
import matplotlib.pyplot as plt
subjects = ["Maths", "Science", "English", "CS"]
marks = [85, 78, 92, 88]
plt.bar(subjects, marks)
plt.title("Marks in Different Subjects")
plt.xlabel("Subjects")
plt.ylabel("Marks")
plt.show()
To place the category names on the x-axis, the tick_label argument is often used, or plt.xticks() is called:
plt.bar([1, 2, 3, 4], marks, tick_label=subjects)
Horizontal bars can be drawn using plt.barh().
A pie chart shows the proportion of each category as a slice of a circle. The angles of the slices are proportional to the values.
import matplotlib.pyplot as plt
expenses = [5000, 3000, 4000, 2000]
labels = ["Rent", "Food", "Travel", "Savings"]
plt.pie(expenses, labels=labels, autopct="%1.1f%%")
plt.title("Monthly Expenses")
plt.show()
labels names each slice.autopct="%1.1f%%" displays each slice's percentage with one decimal place.explode, shadow, and startangle customise the look.plt.pie(expenses, labels=labels, autopct="%1.1f%%", explode=[0.1, 0, 0, 0])
A legend identifies each series or category in the chart. The label argument is set while plotting, and plt.legend() renders the legend box.
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [10, 20, 30, 40]
y2 = [15, 25, 35, 45]
plt.plot(x, y1, label="Class A")
plt.plot(x, y2, label="Class B")
plt.title("Comparison of Two Classes")
plt.xlabel("Days")
plt.ylabel("Scores")
plt.legend()
plt.show()
plt.scatter()) shows the relationship between two variables by plotting individual points. It is used to detect correlation.plt.hist()) groups continuous data into bins and shows the frequency distribution using bars.import matplotlib.pyplot as plt
height = [150, 155, 160, 162, 168, 172, 175, 180]
weight = [45, 50, 55, 58, 62, 68, 70, 75]
plt.scatter(height, weight)
plt.title("Height vs Weight")
plt.xlabel("Height (cm)")
plt.ylabel("Weight (kg)")
plt.show()
Examination questions often ask what a chart conveys. The skills needed are: - Read the title to know what the chart is about. - Read the axes to understand the units and range. - Compare bar heights or line positions to identify the highest, lowest, and trends. - In pie charts, compare slice sizes and percentages to find proportions.
Choosing the right chart is the first step towards honest and effective communication of data. A line chart connects successive points, so it draws the eye along a direction and therefore shows the rise and fall of a quantity over time. When the examiner asks for a chart of the population of a town over the last ten years, the trend is best captured by a line chart because the horizontal axis has a natural order. A bar chart, in contrast, treats the categories on the x-axis as separate boxes with no order, so it is ideal for comparing the marks of different subjects, the sales of different products, or the populations of different states. If the same bar chart data were plotted as a line chart, the connecting lines would imply a relationship between categories that does not exist, which is misleading.
A pie chart is used only when the parts together form a meaningful whole. The total of the values should represent the entire quantity, such as a family's total monthly expenditure. When the parts do not add up to a whole, or when the differences between the parts are very small, a bar chart is usually clearer because readers compare lengths far more accurately than angles. Histograms and scatter plots answer different questions again: a histogram groups continuous measurements into ranges and shows how often each range occurs, while a scatter plot places one point per individual observation and reveals whether two variables move together. Being able to justify the choice of a chart in words is a skill that examiners reward, and it shows a genuine understanding of what each visualisation actually communicates rather than a mere memorisation of function names.
| Function | Purpose |
|---|---|
plt.plot(x, y) |
Draw a line chart |
plt.bar(x, y) |
Draw a vertical bar chart |
plt.barh(x, y) |
Draw a horizontal bar chart |
plt.pie(data) |
Draw a pie chart |
plt.scatter(x, y) |
Draw a scatter plot |
plt.hist(data) |
Draw a histogram |
plt.title(s) |
Set the chart title |
plt.xlabel(s) |
Label the x-axis |
plt.ylabel(s) |
Label the y-axis |
plt.legend() |
Show legend |
plt.grid(True) |
Show gridlines |
plt.show() |
Display the figure |
| Data Type | Purpose | Chart |
|---|---|---|
| Trend over time | Show continuous change | Line chart |
| Compare categories | Compare quantities | Bar chart |
| Part of a whole | Show proportions | Pie chart |
| Two variables | Show relationship | Scatter plot |
| Frequency distribution | Show spread of data | Histogram |
plt.show() in script mode, so no chart is displayed.plt.bar() with plt.hist(); bars are for categories, histograms are for continuous frequency distributions.tick_label for bar charts, which leaves unlabelled bars.plt.plot() for categorical comparison instead of plt.bar().import matplotlib.pyplot as plt.plt.plot(), which raises a TypeError because plot needs numbers.autopct in pie charts, so percentages are not displayed.import matplotlib.pyplot as plt.autopct="%1.1f%%" for percentages in pie charts.Data visualisation turns raw numbers into insight. Using matplotlib's pyplot module, a Python programmer can create line charts to show trends, bar charts to compare categories, pie charts to display proportions, and scatter plots or histograms for relationships and distributions. Every chart needs a clear title, labelled axes, and sometimes a legend to be meaningful. Selecting the correct chart type for the question is as important as writing the code correctly. This visual skill not only earns marks in examinations but also forms the foundation for data science work, where the ability to communicate findings visually is essential.