ЁЯТ╗
тМия╕П
ЁЯЦ▒я╕П
ЁЯЦея╕П
ЁЯТ╛
тЖР Back to Dashboard
Font Size:

1. Introduction

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.

2. Setting up matplotlib

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.

3. Creating a Line Chart

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="--")

4. Creating a Bar Chart

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().

5. Creating a Pie Chart

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()
plt.pie(expenses, labels=labels, autopct="%1.1f%%", explode=[0.1, 0, 0, 0])

6. Adding a Legend

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()

7. Scatter Plot and Histogram

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()

8. Reading and Interpreting Charts

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.

9. Why Chart Selection Matters

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.

Quick Revision Tables

Table 1: Matplotlib Functions

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

Table 2: Choosing the Right Chart

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

Mind Map

graph TD A["Data Visualization"] --> B["matplotlib.pyplot"] A --> C["Chart Types"] A --> D["Customisation"] A --> E["Interpretation"] B --> B1["import matplotlib.pyplot as plt"] C --> C1["Line chart: plot()"] C --> C2["Bar chart: bar(), barh()"] C --> C3["Pie chart: pie()"] C --> C4["Scatter: scatter()"] C --> C5["Histogram: hist()"] D --> D1["title, xlabel, ylabel"] D --> D2["legend, grid"] D --> D3["color, marker, linestyle"] E --> E1["Read title, axes, labels"] E --> E2["Compare values and trends"]

Important Diagrams (SVG)

Diagram 1: Anatomy of a Line Chart

Anatomy of a Line Chart Students Over the Years (Title) 2019 2020 2021 2022 2023 Years (x-axis label) Students (y-axis label) Legend: Students Golden Rule Always label title and both axes so the chart is readable without guesswork

Diagram 2: Selecting the Right Chart

Choosing the Right Chart Trend over time Line chart: plot() e.g. Sales over years Compare categories Bar chart: bar() e.g. Marks per subject Part of a whole Pie chart: pie() e.g. Monthly expenses Two-variable relationship Scatter plot: scatter() e.g. Height vs Weight Frequency distribution Histogram: hist() e.g. Distribution of marks Golden Rule Match the chart type to the question: trends, comparisons, proportions, or distributions

Common Mistakes

  1. Forgetting to call plt.show() in script mode, so no chart is displayed.
  2. Confusing plt.bar() with plt.hist(); bars are for categories, histograms are for continuous frequency distributions.
  3. Not setting tick_label for bar charts, which leaves unlabelled bars.
  4. Using plt.plot() for categorical comparison instead of plt.bar().
  5. Missing the import statement import matplotlib.pyplot as plt.
  6. Using a list of strings for the y-axis in plt.plot(), which raises a TypeError because plot needs numbers.
  7. Forgetting autopct in pie charts, so percentages are not displayed.
  8. Drawing multiple plots in one cell without separating figures, causing overlapping charts.

Exam Tips

  1. Memorise the purpose of each chart type and be able to state when each is used.
  2. Remember the alias: import matplotlib.pyplot as plt.
  3. Learn the exact output program structure: import, data, chart function, labels, title, show.
  4. Practise writing programs for line, bar, and pie charts from given data.
  5. Remember autopct="%1.1f%%" for percentages in pie charts.
  6. For interpretation questions, read the title and axes first and describe the trend, highest value, and lowest value.

Conclusion

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.