Ch-4 Plotting Data Using Matplotlib

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 32

NCERT Solutions Chapter 4 Plotting Data

using Matplotlib
1. What is the purpose of the Matplotlib library?
o The purpose of Matplotlin Library is to present data on graphs.
o You can create, animate interactive 2D plots or figures by using
Matplolib library.
o By plotting data you can visualize variation or show the relationships
between various data elements.
2. What are some of the major components of any graphs or plot?
o Some of the major components of any graphs or plot are:
 Plot Area or Figure or Chart Area
 Legend
 X Axis
 Y Axis
 Plot Title
 ticks
 Artists
 If you want to read in detail about these components, click over
here.
3. Name the function which is used to save the plot.
o savefig()
4. Write short notes on different customization options available with any
plot.
o Chart customization refers to the process of changing the chart
components’ styles, design and adds more attractive features to the
graph.
o The most common options used to customize the plot are as following:
 title() – Applies titles for graphs with font size and color
 grid() – Show/Hide the gridlines on plot
 legend(): Represents the data displayed in the graph’s Y-axis
 loc– Specify the location of the legend as “upper left” or “upper
right”, “lower left”,”lower right”
 plot(): Allows changing the linestyle, linewidth, color,
marker ,marker size etc.
 xlabel(), ylabel(): Specify the x and the y titles respectively you
can change the fontsize and color
 show(): displays the plot
 savefig(): saves the plot at the specified location
5. What is the purpose of a legend?
o Legend represents the data displayed in the graph’s Y-axis.
o It helps to understand what the colors and shapes in the graph mean in
terms of data.
6. Define Pandas visualization.
o Pandas Visualization refers to the process of presenting data in
pictorial or graphical form.
o Visualization helps in a better and depth understanding of numerical
data.
o Pandas support visualization libraries and packages which is easy to
use.
o Pandas visualization also offers methods and properties to create
graphs.
o Pandas offer a single and convenient place to plot graphs i.e. matplotlib
for visualization and data analysis through graphs.
7. What is open data? Name any two websites from which we can
download open data.
o Open data refers to the data available on the internet to use them free
without any license.
o These open data can be used for data analysis primarily for
educational purposes.
o The two websites from where you can download data are:
 data.gov.in
 kaggle.com
8. Give an example of data comparison where we can use the scatter plot.
o The scatter plot is used to determine the association between two data
variables.
o It shows the correlation between two variables.
o It can be also used for comparison between two values.
o An example can be the run rate between two teams in specific overs
o Comparison of marks of two different tests/exams
9. Name the plot which displays the statistical summary.
o To displays the statistical summary you can use bar plot and
histogram.
10.Plot the following data using a line plot:

Day 1 2 3 4 5 6 7

Tickets Sold 2000 2800 3000 2500 2300 2500 1000

 Before displaying the plot display “Monday, Tuesday, Wednesday, Thursday,


Friday, Saturday, Sunday” in place of Day 1, 2, 3, 4, 5, 6, 7
 Change the color of the line to ‘Magenta’.

import pandas as pd
import matplotlib.pyplot as plt

x=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"]

y=[2000,2800,3000,2500,2300,2500,1000]

plt.plot(x,y,color="magenta",marker="*", label="No. of tickets")

plt.title("Day wise Tickets sold ")

plt.xlabel("Days")

plt.ylabel("No of tickets sold")

plt.legend()

plt.grid(True)

plt.savefig("d:\ Tickets.jpg")

plt.show()

11. Collect data about colleges in Delhi University or any other university of
your choice and number of courses they run for Science, Commerce and
Humanities, store it in a CSV file and present it using a bar plot.

import pandas as pd

import matplotlib.pyplot as p

df=pd.read_csv("D:\Python\IP NCERT XII Chapter 4\data.csv", index_col=0)

print(df)

df.plot(kind="bar")
p.title("Stream wise number of courses ")

p.xlabel("Stream")

p.ylabel("No of Courses")

p.xticks(rotation="15")

p.legend()

p.show()

12. Collect and store data related to the screen time of students in your class
separately for boys and girls and present it using a boxplot.

Boys 8 5 6 10 11 9 7 16 2 4

Girls 4 10 11 12 2 3 8 7 13 14

import matplotlib.pyplot as p

boys = [8,5,6,10,11,9,7,16,2,4]

girls=[4,10,11,12,2,3,8,7,13,14]

d=[boys,girls]

p.boxplot(d)

p.show()

13. Explain the findings of the boxplot of Figure 4.18 by filling the following
blanks:
(The figure in the textbook is 4.17)
a) The median for the five subjects is ______________, ___________________,
___________________, _______________, ______________

Ans.:

import pandas as pd
data= pd.read_csv('D:\Python\IP NCERT XII Chapter 4\Q13.csv')
df= pd.DataFrame(data)
print("Median English:",df.loc[:,'English'].median())
print("Median Maths:",df.loc[:,'Maths'].median())
print("Median Hindi:",df.loc[:,'Hindi'].median())
print("Median Science:",df.loc[:,'Science'].median())
print("Median Social_Science:",df.loc[:,'Social_Science'].median())

b) The highest value for the five subjects is : ____________,


_______________________, ________________________,
__________________, ___________

print("Max English:",df.loc[:,'English'].max())
print("Max Maths:",df.loc[:,'Maths'].max())
print("Max Hindi:",df.loc[:,'Hindi'].max())
print("Max Science:",df.loc[:,'Science'].max())
print("Max Social_Science:",df.loc[:,'Social_Science'].max())

c) The lowest value for the five subjects is : ________________,


________________, ___________________, _____________________,
__________

print("Min English:",df.loc[:,'English'].min())
print("Min Maths:",df.loc[:,'Maths'].min())
print("Min Hindi:",df.loc[:,'Hindi'].min())
print("Min Science:",df.loc[:,'Science'].min())
print("Min Social_Science:",df.loc[:,'Social_Science'].min())

d)___________ subject has two outliers with the value ___________________ and
__________________

The Social Science subject has two outliers with the values 54 and 95.
e) _________ subject shows the minimum variation

The Social Science shows the minimum variations the value is 113.064103

14. Collect the minimum and maximum temperature of your city for a month
and present it using a histogram plot.
import pandas as pd
import matplotlib.pyplot as plt
min_temp=[6,5,6,5,5,5,4,9,11,12,14,15,12,13,11,11,8,7,10,10,9,9,7,8,9,5,6,6,7,10]
max_temp=[16,15,15,16,18,19,15,14,18,27,17,20,17,18,15,16,14,17,20,17,18,15,16]
plt.hist(min_temp,bins=5,color="brown",edgecolor="black")
plt.title("Minimum Temperatures in city During Fenruary 2021")
plt.ylabel("Frequency")
plt.xlabel("Temperature")
plt.show()

The similar way you can do for maximum temperature also.

15.Conduct a class census by preparing a questionnaire. The


questionnaire should contain a minimum of five questions. Questions
should relate to students, their family members, their class
performance, their health etc. Each student is required to fill up the
questionnaire. Compile the information in numerical terms (in terms of
percentage). Present the information through a bar, scatter–diagram.
(NCERT Geography class IX, Page 60).

Objective type questions (Plotting with


pyplot Assignments Class-12)
MCQs, Fill in the blanks and True/False
Q – 1 A _________ refers to the graphical representation of data and information
using charts or diagrams or maps.

–> Data Visualization

Q – 2 Data visualization helps to

a) Understand data easily

b) Take a decisions

c) Improve the past performance

d) All of these

Q – 3 The ______ module allows you to represent data visually in various forms.

–> pyplot
Q – 4 The ________ library provides the interface and functionality for plotting the
graphs.

–> matplotlib

Q – 5 Which of the following offers many different names collections of methods?

a) PyPlot

b) matplotlib

c) matlab

d) graphs

Q -6 Which of the following correct statement to import pyplot module?

a) import matplotlib.pyplot

b) import MatPlotLib.PyPlot

c) import PyPlot as pl

d) import pyplot.plot

Q – 7 A __________ chart displays information as a markers connected by a


straight lines.

–> Line

Q – 8 A bar chart is also known as column chart. (True/False)

Q – 9 The entire is covered by the graph is known as __________

–> Figure or Chart Area

Q – 10 Which is a common method used to plot data on the chart?

a) plot()

b) show()

c) legend()
d) title()

Q – 11 Which of the following is/are correct statement for plot method?

a) pl.plot(x,y,color,others)

b) pl.plot(x,y)

c) pl.plot(x,y,color)

d) all of these

Q – 12 What are the mandatory parameters to plot data on chart for plot() method?

a) x and y

b) color

c) others

d) None of these

Q – 13 To give a title to x-axis, which of the following method is useful?

a) pl.xtitle(“title”)

b) pl.xlabel(“title”)

c) pl.xheader(“title”)

d) pl.xlabel.show(“title”)

Q – 14 The pl.show() method must be used to display the chart in the end of the
chart specification. (True/False)

Q – 15 The _________ method is used to create a line chart.

a) pl.pie()

b) pl.col()

c) pl.plot()
d) pl.line()

Q – 16 To create a horizontal bar chart, bar() function is used. (True/False)

Q – 17 To change the width of bars in bar chart, which of the following argument
with a float value is used?

a) thick

b) thickness

c) width

d) barwidth

Q – 18 You can set different width for different bars in bar chart. (True/False)

Q – 19 To apply color you can only specify the color names. (True/False)

Q – 20 Which method is used to display or show the legends?

a) pl.show()

b) pl.display()

c) pl.legend()

d) pl.values()

The following section contains short answer questions for Plotting with pyplot
Assignments Class-12.

Descriptive questions (2/3 marks) –


Plotting with pyplot Assignments
Q – 1 What do you mean by data visualization technique?

The data visualization technique refers to the graphical or pictorial or visual


representation of data. This can be achieved by charts, graphs, diagrams, or maps.

Q – 2 How data visualization can help in decision making?


Most data visualization technique provides data in form of charts or graphs. These
charts are majorly used in producing various reports like trends, outliers, and other
diagrams. By observing these all users can understand the data easily and he/she
can take their decision.

Q – 3 Name the library and interface used to plot a chart in python.

Library – matplotlib

interface – pyplot

Q – 4 What are the ways of importing matplotlib?

You can import matplotlib in following two ways:

1. Using alias name: import matplotlib.pyplot as pp


2. Without alias name: import matplotlib.pyplot

Q -5 What are the basic elements/components of the chart?

The chart has following elements/components:

1. Chart area or figure


2. Axis
3. Artist
4. Titles
5. Legends

Q -6 Write steps to plot your data on a graph.

1. import module i.e import matplolib.pyplot as pp


2. Choose the desired chart type to plot data. For ex. Line chart
3. Use proper titles for axis
4. Add data points
5. Add more properties to the graph like color, size etc.

Q – 7 What types of graphs can be plotted using matplotlib?

The matplotlib provides following types of charts:

1. Line chart
2. Bar chart
3. Horizontal bar chart
4. Histogram
5. Scatter chart
6. Boxplot
7. Pie Chart

The below given questions from Plotting with pyplot Assignments are based on
applications.

Q – 8 Write code to do the following:

[1] Plot the following data on line chart:

Runs in Overs 10 20

MI 110 224

RCB 85 210

import matplotlib.pyplot as mpp

overs = [10,20]

mi = [110,224]

mpp.plot(overs,mi,'blue')

rcb=[109,210]

mpp.plot(overs,rcb,'red')

mpp.xlabel('Runs')

mpp.ylabel('Overs')

mpp.title('Match Summary')

mpp.show()
[2] Write code to plot a line chart to depict the run rate of T20 match from given data:

Overs Runs

5 45

10 79

15 145

20 234

import matplotlib as pp

overs = [5,10,15,20]

runs = [54,79,145,234]

pp.plot(overs,runs)

pp.xlabel('Overs')

pp.ylabel('Runs')

pp.show()

Q – 9 How to change the thickness of line, line style, line color, and marker
properties of a chart?

To change the thickness of line, use the linewidth parameter inside


matplotlib.pyplot.plot() function with a numeric value. For ex.:
pp.plot(x,y,linewidth=2)

To change the line style, use linestyle or ls parameter. This linestyle can be one of
the following:
1. solid
2. dashed
3. dashdot
4. dotted

Ex. pp.plot(x,y,linestyle=’dashed’)

To change the color use color shortcode like r for red, g for green and so on. You
can also use the complete colornames or hexadecimal color codes like #000800 in
RGB values. Click here to know more about line colors.

To change the markers user marker properties as following:

[1] markertype: It can be a symbol such as . (dot), ‘D’ for diamond etc. Click here for
more about marker types.

Ex.: pp.plot(x,y,marker=’D’)

[2] markersize: It can be a numeric value.

Ex. pp.plot(x,y,marker=’D’,markersize=4)

[3] markeredgecolor: It can be a color shortcode or color name or color code.

Ex. pp.plot(x,y,marker=’D’,markeredgecolor=’blue’)

Q – 10 Plot following data on bar graph:

English: 56,78,90,34

Science: 65,77,54,32

Maths: 45,67,43,41

import matplotlib.pyplot as pp

eng = [56,78,90,34]

sci = [65,77,54,32]

maths =[45,67,43,41]
pp.bar(eng,sci,maths)

pp.xlabel('Marks')

pp.ylabel('Subjects')

pp.show()

Click here to read notes on Data Visualization using pyplot.

CBSE has introduced competency-based questions. In the next section of Plotting


with pyplot Assignments, I am going to cover some competency-based on Plotting
with pyplot Assignments.

Comptency-based questions Plotting with


pyplot Assignments
[1] Mr. Vijay is working in the mobile app development industry and he was
comparing the given chart on the basis of the rating of the various apps
available on the play store.
He is trying to write a code to plot the graph. Help Mr. Vijay to fill in the blanks of the
code and get the desired output.

import ________________ as plt #Statement 1

apps=["Arogya Setu","WPS Office","Cam Scanner","WhatsApp","Telegram"]

ps_rating=[3.9,4.5,4.6,4.2,4.3]

plt.__________(apps,ps_rating,color='m',label=__________) #Statement 2 Statement 3

plt.xlabel("Apps")

plt._____________("Rating") #Statement 4

plt._________ #Statement 5
plt.________ #Statement 6

i) Write the appropriate statement for #statement 1 to import the module.

ii) Write the function name and label name as displayed in the output for #statement
2 and #statement 3 respectively.

iii) Which word should be used for #statement 4?

iv) Write appropriate method names for #Statement 5 to display legends and
#Statement 6 to open the figure.

v) Mr. Vijay wants to change the chart type to a line chart. Which statement should
be updated and which method or function is used?

Ans.:

i) matplotlib.pyplot

ii) bar, App Rating

iii) ylabel

iv) legend(), show()

v) Statement 2 should be changed. It requires plot() method to plot the data.


[2] Mrs. Namrata is a coordinator in the senior section school. She
represented data on number of students who passed the exam on line chart as
follows:

She has written the following code but not getting the desired output. Help her by
correcting her code.

import matplotlib.pyplot as plt

classes=["X A","X B","XI A","XI B","XII A","XII B"]

no_of_boys=[23,22,20,26,33,30]

no_of_girls=[17,10,20,12,5,8]

plt.line(classes,no_of_boys) #Statement 1

plt.line(classes,no_of_girls) #Statement 2

plt.xtitle("No of Stduents") #Statement 3

plt.ytitle("Classes") #Statement 4
plt.show()

i) What will be the correct code for Statement 1 and Statement 2?

ii) What is the correct function name for Statement 3 and Statement 4?

iii) Write a method and parameter required to display legends?

iv) Name the parameter and values used to apply the marker as given in the output.

v) Name the parameter and values used to apply linestyle as given in the output.

vi) How to apply the line colours as given in the figure?

vii) Write to save the figure as image.

Ans.:

i) The code for statement 1 and statement 2 is as follows:

1. Statement 1: plt.plot(classes,no_of_boys)
2. Statement 2:plt.plot(classes,no_of_girls)

ii) The correct code for statement 3 and statement 4 is as follows:

1. plt.xlabel(‘classes’)
2. plt.ylabel(‘No of stduents’)

iii) To display the legend she need to add label parameter in the plot method as
following:

1. plt.plot(classes,no_of_boys,label=’Boys’)
2. plt.plot(classes,no_of_girls,label=’Girls’)

To display legend she need to write this function:

plt.legend()

iv) To apply the marker as given in the figure she needs to write the code as follows:

plt.plot(classes,no_of_boys,marker='D')

plt.plot(classes,no_of_girls,marker='o')
v) To apply the lines styles she needs to use a parameter linestyle or ls with the
values ‘dashed’ and ‘dotted’ respectively as follows:

plt.plot(classes,no_of_boys,ls='dashed')

plt.plot(classes,no_of_girls,ls='dotted')

vi) She can use color parameter to apply colours as follows:

plt.plot(classes,no_of_boys,color='m')

plt.plot(classes,no_of_girls,color='pink')

vii) To save the figure as image she needs to use savefig() method as follows:

plt.savefig('boygirlspass.jpg')

[3]

(i) Consider the following statements with reference to Line charts:

a) Line chart is a tool for comparison and is created by plotting a series of several
points connecting them with a straight line.

b) You should never use a line chart when the chart is in a continuous data set.

Which of the above statement(s) are true?

(ii) Consider the following statements with reference to data visualization:

a) Graphical representation of information and data

b) Helps users in analyzing a large amount of data in a simpler way

c) Data visualization makes complex data more accessible, understandable and


usable

d) No library needs to be imported to use charts in python

Which of the statement(s) are not true?


(iii) Consider these statements:

a) The figure() attribute is used to save the plot created by python

b) The filename for saving the plot must be written with a complete path including an
extension

Which the following statement(s) are true?

Ans.:

(i) a) is correct , b) is incorrect

(ii) d) No library needs to be imported to use charts in python

(iii) a) and b) both are incorrect

Important Programs Plotting with Python Class 12

In this section of important programs plotting with python class 12, we will cover
practicals to plot a single line chart.

[1] Plot following data on line chart:

Day Monday Tuesday Wednesday Thursday Friday

Income 510 350 475 580 600

1. Write a title for the chart “The Weekly Income Report”.


2. Write the appropriate titles of both the axes.
3. Write code to Display legends.
4. Display red color for the line.
5. Use the line style – dashed
6. Display diamond style markers on data points

import matplotlib.pyplot as pp

day =['Monday','Tuesday','Wednesday','Thursday','Friday']

inc = [510,350,475,580,600]

pp.plot(day,inc,label='Income',color='r',linestyle='dashed',marker='D')
pp.title("The Weekly Income Report")

pp.xlabel("Days")

pp.ylabel("Income")

pp.legend()

pp.show()

Output:

[2] A Shivalik restaurant has recorded the following data into their register for their
income by Drinks and Food. Plot them on the line chart.

Day Monday Tuesday Wednesday Thursday Friday

Drinks 450 560 400 605 580

Food 490 600 425 610 625

Apply following customization to the line chart.

1. Write a title for the chart “The Weekly Restaurant Orders”.


2. Write the appropriate titles of both the axes.
3. Write code to Display legends.
4. Display your choice of colors for both the lines drinks and food.
5. Use the line style – dotted for drinks and dashdot for food.
6. Display plus markers on drinks and x markers of food.

import matplotlib.pyplot as pp

day =['Monday','Tuesday','Wednesday','Thursday','Friday']

dr = [450,560,400,605,580]

fd = [490,600,425,610,625]

pp.plot(day,dr,label='Drinks',color='g',linestyle='dotted',marker='+')

pp.plot(day,fd,label='Food',color='m',linestyle='dashdot',marker='x')

pp.title("The Weekly Restaurant Orders")

pp.xlabel("Days")

pp.ylabel("Orders")

pp.legend()

pp.show()

Output:
[3] Observe the given data for monthly views of one of the youtube channels for 6
months. Plot them on the line chart.

Month January February March April May June

Views 2500 2100 1700 3500 3000 3800

Apply following customizations to the chart:

1. Give the title for the chart – “Youtube Stats”


2. Use the “Month” label for X-Axis and “Views” for Y-Axis.
3. Display legends.
4. Use dashed lines with the width 5 point.
5. Use red color for the line.
6. Use dot marker with blue edge color and black fill color.

import matplotlib.pyplot as pp

mon =['January','February','March','April','May','June']
views = [2500,2100,1700,3500,3000,3800]

pp.plot(mon,views,label='Views State',color='r',linestyle='dashed', linewidth=4,\

marker='o', markerfacecolor='k', markeredgecolor='b')

pp.title("Youtube Stats")

pp.xlabel("Months")

pp.ylabel("Views")

pp.legend()

pp.show()

Output:

[4] Consider the following data of a medical store and plot the data on the line chart:
Month Masks Sanitizer Hand wash

March 1500 4400 6500

April 3500 4500 5000

May 6500 5500 5800

June 6700 6000 6300

July 6000 5600 6200

August 6800 6300 4500

Customize the chart as you wish.

import matplotlib.pyplot as pp

mon =['March','April','May','June','July','August']

mask= [1500,3500,6500,6700,6000,6800]

san = [4400,4500,5500,6000,5600,6300]

hw = [6500,5000,5800,6300,6200,4500]

pp.plot(mon,mask,label='Mask',color='g',linestyle='dashed', linewidth=4,\

marker='o', markerfacecolor='k', markeredgecolor='r')

pp.plot(mon,san,label='Mask',color='b',linestyle='dashed', linewidth=4,\

marker='3', markerfacecolor='k', markeredgecolor='g')

pp.plot(mon,hw,label='Mask',color='r',linestyle='dashed', linewidth=4,\
marker='v', markerfacecolor='k', markeredgecolor='b')

pp.title("Fitwell Medical Store")

pp.xlabel("Months")

pp.ylabel("Covid Protections")

pp.legend()

pp.show()

Output:

[5] Use above data and subplot sanitizer data and handwash data.

import matplotlib.pyplot as pp
mon =['March','April','May','June','July','August']

san = [4400,4500,5500,6000,5600,6300]

hw = [6500,5000,5800,6300,6200,4500]

pp.subplot(2,1,1)

pp.plot(mon,san,label='Sanitizer',color='b',linestyle='dashed', linewidth=4,\

marker='o', markerfacecolor='k', markeredgecolor='r')

pp.title("Fitwell Medical Store")

pp.legend()

pp.subplot(2,1,2)

pp.plot(mon,hw,label='Handwash',color='r',linestyle='dashed', linewidth=4,\

marker='v', markerfacecolor='k', markeredgecolor='b')

pp.xlabel("Months")

pp.ylabel("Covid Protections")

pp.legend()

pp.show()

Output:
[6] Write a program to plot a range from 1 to 30 with step value 4. Use the following
algebraic expression to show data.

y = 5*x+2

import matplotlib.pyplot as pp

import numpy as np

x = np.arange(1,30,4)

y=5*x+2

pp.plot(x,y)

pp.show()

Output:
[7] Display following bowling figures through bar chart:

Overs Runs

1 6

2 18

3 10

4 5

import matplotlib.pyplot as pp
overs =[1,2,3,4]

runs=[6,18,10,5]

pp.bar(overs,runs,color='m')

pp.xlabel('Overs')

pp.xlabel('Runs')

pp.title('Bowling Spell Analysis')

pp.xticks([1,2,3,4])

pp.yticks([5,10,15,20])

pp.show()

Output:
[8] Observe following data and plot data according to given instructions:

Batsman 2017 2018 2019 2020

Virat Kohli 2501 1855 2203 1223

Steve Smith 2340 2250 2003 1153

Babar Azam 1750 2147 1896 1008

Rohit Sharma 1463 1985 1854 1638

Kane Williamson 1256 1785 1874 1974

Jos Butler 1125 1853 1769 1436

1. Create a bar chart to display data of Virat Kohli & Rohit Sharma.
o Customize the chart in this manner
 Use different widths
 Use different colors to represent different years score
 Display appropriate titles for axis and chart
 Show legends
2. Create a bar chart to display data of Steve Smith, Kane Williamson & Jos
Butler. Customize Chart as per your wish.
3. Display data of all players for the specific year.
Destress by colouring in Mandela Art

You might also like