Chapter
Plotting Data using
4 Matplotlib
“Human visual perception is the
“most powerful of data interfaces
between computers and Humans”
— M. McIntyre
In this chapter
»» Introduction
»» Plotting using
Matplotlib
4.1 Introduction »» Customisation of
We have learned how to organise and analyse Plots
data and perform various statistical operations »» The Pandas Plot
on Pandas DataFrames. Likewise, in Class XI, we Function (Pandas
have learned how to analyse numerical data using Visualisation)
NumPy. The results obtained after analysis is used
to make inferences or draw conclusions about data
as well as to make important business decisions.
Sometimes, it is not easy to infer by merely looking
at the results. In such cases, visualisation helps
in better understanding of results of the analysis.
Data visualisation means graphical or pictorial
representation of the data using graph, chart,
etc. The purpose of plotting data is to visualise
variation or show relationships between variables.
2024-25
Chapter 4.indd 105 10/9/2020 12:35:3
, 106 Informatics Practices
Notes Visualisation also helps to effectively communicate
information to intended users. Traffic symbols,
ultrasound reports, Atlas book of maps, speedometer
of a vehicle, tuners of instruments are few examples
of visualisation that we come across in our daily lives.
Visualisation of data is effectively used in fields like
health, finance, science, mathematics, engineering, etc.
In this chapter, we will learn how to visualise data using
Matplotlib library of Python by plotting charts such
as line, bar, scatter with respect to the various types
of data.
4.2 Plotting using Matplotlib
Matplotlib library is used for creating static, animated,
and interactive 2D- plots or figures in Python. It can
be installed using the following pip command from the
command prompt:
pip install matplotlib
For plotting using Matplotlib, we need to import its
Pyplot module using the following command:
import matplotlib.pyplot as plt
Here, plt is an alias or an alternative name for
matplotlib.pyplot. We can use any other alias also.
Figure 4.1: Components of a plot
The pyplot module of matplotlib contains a collection
of functions that can be used to work on a plot. The
plot() function of the pyplot module is used to create a
figure. A figure is the overall window where the outputs
of pyplot functions are plotted. A figure contains a
2024-25
Chapter 4.indd 106 10/9/2020 12:35:3
, Plotting Data using Matplotlib 107
plotting area, legend, axis labels, ticks, title, etc. (Figure Notes
4.1). Each function makes some change to a figure:
example, creates a figure, creates a plotting area in a
figure, plots some lines in a plotting area, decorates the
plot with labels, etc.
It is always expected that the data presented through
charts easily understood. Hence, while presenting data
we should always give a chart title, label the axis of the
chart and provide legend in case we have more than one
plotted data.
To plot x versus y, we can write plt.plot(x,y). The
show() function is used to display the figure created
using the plot() function.
Let us consider that in a city, the maximum temperature
of a day is recorded for three consecutive days. Program
4-1 demonstrates how to plot temperature values for
the given dates. The output generated is a line chart.
Program 4-1 Plotting Temperature against Height
import matplotlib.pyplot as plt
#list storing date in string format
date=["25/12","26/12","27/12"]
#list storing temperature values
temp=[8.5,10.5,6.8]
#create a figure plotting temp versus date
plt.plot(date, temp)
#show the figure
plt.show()
Figure 4.2: Line chart as output of Program 4-1
2024-25
Chapter 4.indd 107 10/9/2020 12:35:3
, 108 Informatics Practices
In program 4-1, plot() is provided with two parameters,
which indicates values for x-axis and y-axis, respectively.
The x and y ticks are displayed accordingly. As shown
in Figure 4.2, the plot() function by default plots a line
chart. We can click on the save button on the output
window and save the plot as an image. A figure can also
be saved by using savefig() function. The name of the
figure is passed to the function as parameter.
For example: plt.savefig('x.png').
In the previous example, we used plot() function
to plot a line graph. There are different types of data
available for analysis. The plotting methods allow for a
handful of plot types other than the default line plot, as
listed in Table 4.1. Choice of plot is determined by the
type of data we have.
Table 4.1 List of Pyplot functions to plot different charts
plot(\*args[, scalex, scaley, data]) Plot x versus y as lines and/or markers.
bar(x, height[, width, bottom, align, data]) Make a bar plot.
boxplot(x[, notch, sym, vert, whis, ...]) Make a box and whisker plot.
hist(x[, bins, range, density, weights, ...]) Plot a histogram.
pie(x[, explode, labels, colors, autopct, ...]) Plot a pie chart.
scatter(x, y[, s, c, marker, cmap, norm, ...]) A scatter plot of x versus y.
4.3 Customisation of Plots
Pyplot library gives us numerous functions, which can
be used to customise charts such as adding titles or
legends. Some of the customisation options are listed in
Table 4.2:
Table 4.2 List of Pyplot functions to customise plots
grid([b, which, axis]) Configure the grid lines.
legend(\*args, \*\*kwargs) Place a legend on the axes.
savefig(\*args, \*\*kwargs) Save the current figure.
show(\*args, \*\*kw) Display all figures.
title(label[, fontdict, loc, pad]) Set a title for the axes.
xlabel(xlabel[, fontdict, labelpad]) Set the label for the x-axis.
xticks([ticks, labels]) Get or set the current tick locations and labels of the x-axis.
ylabel(ylabel[, fontdict, labelpad]) Set the label for the y-axis.
yticks([ticks, labels]) Get or set the current tick locations and labels of the y-axis.
2024-25
Chapter 4.indd 108 10/9/2020 12:35:3
Plotting Data using
4 Matplotlib
“Human visual perception is the
“most powerful of data interfaces
between computers and Humans”
— M. McIntyre
In this chapter
»» Introduction
»» Plotting using
Matplotlib
4.1 Introduction »» Customisation of
We have learned how to organise and analyse Plots
data and perform various statistical operations »» The Pandas Plot
on Pandas DataFrames. Likewise, in Class XI, we Function (Pandas
have learned how to analyse numerical data using Visualisation)
NumPy. The results obtained after analysis is used
to make inferences or draw conclusions about data
as well as to make important business decisions.
Sometimes, it is not easy to infer by merely looking
at the results. In such cases, visualisation helps
in better understanding of results of the analysis.
Data visualisation means graphical or pictorial
representation of the data using graph, chart,
etc. The purpose of plotting data is to visualise
variation or show relationships between variables.
2024-25
Chapter 4.indd 105 10/9/2020 12:35:3
, 106 Informatics Practices
Notes Visualisation also helps to effectively communicate
information to intended users. Traffic symbols,
ultrasound reports, Atlas book of maps, speedometer
of a vehicle, tuners of instruments are few examples
of visualisation that we come across in our daily lives.
Visualisation of data is effectively used in fields like
health, finance, science, mathematics, engineering, etc.
In this chapter, we will learn how to visualise data using
Matplotlib library of Python by plotting charts such
as line, bar, scatter with respect to the various types
of data.
4.2 Plotting using Matplotlib
Matplotlib library is used for creating static, animated,
and interactive 2D- plots or figures in Python. It can
be installed using the following pip command from the
command prompt:
pip install matplotlib
For plotting using Matplotlib, we need to import its
Pyplot module using the following command:
import matplotlib.pyplot as plt
Here, plt is an alias or an alternative name for
matplotlib.pyplot. We can use any other alias also.
Figure 4.1: Components of a plot
The pyplot module of matplotlib contains a collection
of functions that can be used to work on a plot. The
plot() function of the pyplot module is used to create a
figure. A figure is the overall window where the outputs
of pyplot functions are plotted. A figure contains a
2024-25
Chapter 4.indd 106 10/9/2020 12:35:3
, Plotting Data using Matplotlib 107
plotting area, legend, axis labels, ticks, title, etc. (Figure Notes
4.1). Each function makes some change to a figure:
example, creates a figure, creates a plotting area in a
figure, plots some lines in a plotting area, decorates the
plot with labels, etc.
It is always expected that the data presented through
charts easily understood. Hence, while presenting data
we should always give a chart title, label the axis of the
chart and provide legend in case we have more than one
plotted data.
To plot x versus y, we can write plt.plot(x,y). The
show() function is used to display the figure created
using the plot() function.
Let us consider that in a city, the maximum temperature
of a day is recorded for three consecutive days. Program
4-1 demonstrates how to plot temperature values for
the given dates. The output generated is a line chart.
Program 4-1 Plotting Temperature against Height
import matplotlib.pyplot as plt
#list storing date in string format
date=["25/12","26/12","27/12"]
#list storing temperature values
temp=[8.5,10.5,6.8]
#create a figure plotting temp versus date
plt.plot(date, temp)
#show the figure
plt.show()
Figure 4.2: Line chart as output of Program 4-1
2024-25
Chapter 4.indd 107 10/9/2020 12:35:3
, 108 Informatics Practices
In program 4-1, plot() is provided with two parameters,
which indicates values for x-axis and y-axis, respectively.
The x and y ticks are displayed accordingly. As shown
in Figure 4.2, the plot() function by default plots a line
chart. We can click on the save button on the output
window and save the plot as an image. A figure can also
be saved by using savefig() function. The name of the
figure is passed to the function as parameter.
For example: plt.savefig('x.png').
In the previous example, we used plot() function
to plot a line graph. There are different types of data
available for analysis. The plotting methods allow for a
handful of plot types other than the default line plot, as
listed in Table 4.1. Choice of plot is determined by the
type of data we have.
Table 4.1 List of Pyplot functions to plot different charts
plot(\*args[, scalex, scaley, data]) Plot x versus y as lines and/or markers.
bar(x, height[, width, bottom, align, data]) Make a bar plot.
boxplot(x[, notch, sym, vert, whis, ...]) Make a box and whisker plot.
hist(x[, bins, range, density, weights, ...]) Plot a histogram.
pie(x[, explode, labels, colors, autopct, ...]) Plot a pie chart.
scatter(x, y[, s, c, marker, cmap, norm, ...]) A scatter plot of x versus y.
4.3 Customisation of Plots
Pyplot library gives us numerous functions, which can
be used to customise charts such as adding titles or
legends. Some of the customisation options are listed in
Table 4.2:
Table 4.2 List of Pyplot functions to customise plots
grid([b, which, axis]) Configure the grid lines.
legend(\*args, \*\*kwargs) Place a legend on the axes.
savefig(\*args, \*\*kwargs) Save the current figure.
show(\*args, \*\*kw) Display all figures.
title(label[, fontdict, loc, pad]) Set a title for the axes.
xlabel(xlabel[, fontdict, labelpad]) Set the label for the x-axis.
xticks([ticks, labels]) Get or set the current tick locations and labels of the x-axis.
ylabel(ylabel[, fontdict, labelpad]) Set the label for the y-axis.
yticks([ticks, labels]) Get or set the current tick locations and labels of the y-axis.
2024-25
Chapter 4.indd 108 10/9/2020 12:35:3