Humanities/Arts Exam  >  Humanities/Arts Notes  >  Informatics Practices for Class 12  >  NCERT Textbook: Plotting Data using Matplotlib

NCERT Textbook: Plotting Data using Matplotlib | Informatics Practices for Class 12 - Humanities/Arts PDF Download

Download, print and study this document offline
Please wait while the PDF view is loading
 Page 1


4.1 Introduct Ion We have learned how to organise and analyse 
data and perform various statistical operations 
on Pandas DataFrames. Likewise, in Class XI, we 
have learned how to analyse numerical data using 
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. 
“Human visual perception is the 
“most powerful of data interfaces 
between computers and Humans”
— M. McIntyre
Chapter
 4
Plotting Data using 
Matplotlib
In this chapter
 » Introduction
 » Plotting using 
Matplotlib
 » Customisation of 
Plots
 » The Pandas Plot 
Function (Pandas 
Visualisation)
Chapter 4.indd   105 10/9/2020   12:35:31 PM
2024-25
Page 2


4.1 Introduct Ion We have learned how to organise and analyse 
data and perform various statistical operations 
on Pandas DataFrames. Likewise, in Class XI, we 
have learned how to analyse numerical data using 
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. 
“Human visual perception is the 
“most powerful of data interfaces 
between computers and Humans”
— M. McIntyre
Chapter
 4
Plotting Data using 
Matplotlib
In this chapter
 » Introduction
 » Plotting using 
Matplotlib
 » Customisation of 
Plots
 » The Pandas Plot 
Function (Pandas 
Visualisation)
Chapter 4.indd   105 10/9/2020   12:35:31 PM
2024-25
Informat Ics Pract Ices 106
 
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 Plott Ing us Ing Mat Plotl Ib 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 
n otes Chapter 4.indd   106 10/9/2020   12:35:31 PM
2024-25
Page 3


4.1 Introduct Ion We have learned how to organise and analyse 
data and perform various statistical operations 
on Pandas DataFrames. Likewise, in Class XI, we 
have learned how to analyse numerical data using 
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. 
“Human visual perception is the 
“most powerful of data interfaces 
between computers and Humans”
— M. McIntyre
Chapter
 4
Plotting Data using 
Matplotlib
In this chapter
 » Introduction
 » Plotting using 
Matplotlib
 » Customisation of 
Plots
 » The Pandas Plot 
Function (Pandas 
Visualisation)
Chapter 4.indd   105 10/9/2020   12:35:31 PM
2024-25
Informat Ics Pract Ices 106
 
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 Plott Ing us Ing Mat Plotl Ib 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 
n otes Chapter 4.indd   106 10/9/2020   12:35:31 PM
2024-25
Plotting Data using Mat Plotlib 107
plotting area, legend, axis labels, ticks, title, etc. (Figure 
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
n otes Chapter 4.indd   107 10/9/2020   12:35:32 PM
2024-25
Page 4


4.1 Introduct Ion We have learned how to organise and analyse 
data and perform various statistical operations 
on Pandas DataFrames. Likewise, in Class XI, we 
have learned how to analyse numerical data using 
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. 
“Human visual perception is the 
“most powerful of data interfaces 
between computers and Humans”
— M. McIntyre
Chapter
 4
Plotting Data using 
Matplotlib
In this chapter
 » Introduction
 » Plotting using 
Matplotlib
 » Customisation of 
Plots
 » The Pandas Plot 
Function (Pandas 
Visualisation)
Chapter 4.indd   105 10/9/2020   12:35:31 PM
2024-25
Informat Ics Pract Ices 106
 
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 Plott Ing us Ing Mat Plotl Ib 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 
n otes Chapter 4.indd   106 10/9/2020   12:35:31 PM
2024-25
Plotting Data using Mat Plotlib 107
plotting area, legend, axis labels, ticks, title, etc. (Figure 
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
n otes Chapter 4.indd   107 10/9/2020   12:35:32 PM
2024-25
Informat Ics Pract Ices 108
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 c usto MIsat Ion 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.
Chapter 4.indd   108 10/9/2020   12:35:32 PM
2024-25
Page 5


4.1 Introduct Ion We have learned how to organise and analyse 
data and perform various statistical operations 
on Pandas DataFrames. Likewise, in Class XI, we 
have learned how to analyse numerical data using 
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. 
“Human visual perception is the 
“most powerful of data interfaces 
between computers and Humans”
— M. McIntyre
Chapter
 4
Plotting Data using 
Matplotlib
In this chapter
 » Introduction
 » Plotting using 
Matplotlib
 » Customisation of 
Plots
 » The Pandas Plot 
Function (Pandas 
Visualisation)
Chapter 4.indd   105 10/9/2020   12:35:31 PM
2024-25
Informat Ics Pract Ices 106
 
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 Plott Ing us Ing Mat Plotl Ib 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 
n otes Chapter 4.indd   106 10/9/2020   12:35:31 PM
2024-25
Plotting Data using Mat Plotlib 107
plotting area, legend, axis labels, ticks, title, etc. (Figure 
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
n otes Chapter 4.indd   107 10/9/2020   12:35:32 PM
2024-25
Informat Ics Pract Ices 108
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 c usto MIsat Ion 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.
Chapter 4.indd   108 10/9/2020   12:35:32 PM
2024-25
Plotting Data using Mat Plotlib 109
Program 4-2   Plotting a line chart of date versus temperature 
by adding Label on X and Y axis, and adding a 
Title and Grids to the chart.
import matplotlib.pyplot as plt
date=["25/12","26/12","27/12"]
temp=[8.5,10.5,6.8]
plt.plot(date, temp)
plt.xlabel("Date")    #add the Label on x-axis
plt.ylabel("Temperature")   #add the Label on y-axis
plt.title("Date wise Temperature")  #add the title to the chart
plt.grid(True)    #add gridlines to the background
plt.yticks(temp)     
plt.show()
Figure 4.3: Line chart as output of Program 4-2
In the above example, we have used the xlabel, ylabel, 
title and yticks functions. We can see that compared 
to Figure 4.2, the Figure 4.3 conveys more meaning, 
easily. We will learn about customisation of other plots 
in later sections.
4.3.1 Marker
We can make certain other changes to plots by passing 
various parameters to the plot() function. In Figure 
4.3, we plot temperatures day-wise. It is also possible 
to specify each point in the line through a marker.  
On providing a single 
list or array to the 
plot() function, can 
matplotlib generate 
values for both the x 
and y axis?
Think and Reflect
Chapter 4.indd   109 10/9/2020   12:35:34 PM
2024-25
Read More
14 docs

FAQs on NCERT Textbook: Plotting Data using Matplotlib - Informatics Practices for Class 12 - Humanities/Arts

1. What is Matplotlib and why is it used in data plotting?
Ans. Matplotlib is a popular Python library used for creating static, animated, and interactive visualizations in data science. It provides a wide range of plotting functions and features that enable users to create detailed graphs and charts from data. It is particularly useful for visualizing data trends, distributions, and relationships, making it an essential tool for data analysis in various fields, including humanities and arts.
2. How do I install Matplotlib in Python?
Ans. To install Matplotlib, you can use pip, the Python package manager. Open your command line or terminal and type the following command: `pip install matplotlib`. This will download and install the latest version of Matplotlib. If you are using Anaconda, you can also install it via the Anaconda Navigator or by running `conda install matplotlib` in the Anaconda prompt.
3. What types of plots can I create using Matplotlib?
Ans. Matplotlib allows you to create a variety of plots, including line plots, bar charts, histograms, scatter plots, pie charts, and more. Each type of plot can be customized with different colors, markers, and styles to effectively represent your data. This versatility makes Matplotlib suitable for a wide range of data visualization needs.
4. Can I customize the appearance of my plots in Matplotlib?
Ans. Yes, Matplotlib offers extensive customization options for your plots. You can change the colors, line styles, markers, labels, and titles. Additionally, you can adjust axis limits, add grid lines, and annotate points on the graph. This level of customization allows you to create visually appealing and informative plots tailored to your specific requirements.
5. How can I save my plotted graphs in Matplotlib?
Ans. To save your plotted graphs in Matplotlib, you can use the `savefig()` function. After creating your plot, simply call `plt.savefig('filename.png')` where 'filename.png' is the name you wish to give your image file. You can also specify different formats such as .jpg, .pdf, or .svg by changing the file extension. This allows you to keep a permanent record of your visualizations.
Related Searches

NCERT Textbook: Plotting Data using Matplotlib | Informatics Practices for Class 12 - Humanities/Arts

,

Exam

,

NCERT Textbook: Plotting Data using Matplotlib | Informatics Practices for Class 12 - Humanities/Arts

,

Summary

,

Objective type Questions

,

pdf

,

ppt

,

MCQs

,

mock tests for examination

,

Viva Questions

,

Previous Year Questions with Solutions

,

Semester Notes

,

Free

,

NCERT Textbook: Plotting Data using Matplotlib | Informatics Practices for Class 12 - Humanities/Arts

,

Sample Paper

,

video lectures

,

practice quizzes

,

study material

,

past year papers

,

Extra Questions

,

Important questions

,

shortcuts and tricks

;