Ch-4 Plotting Data Using Matplotlib
Ch-4 Plotting Data Using Matplotlib
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
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.xlabel("Days")
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
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())
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())
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()
b) Take a decisions
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
a) PyPlot
b) matplotlib
c) matlab
d) graphs
a) import matplotlib.pyplot
b) import MatPlotLib.PyPlot
c) import PyPlot as pl
d) import pyplot.plot
–> Line
a) plot()
b) show()
c) legend()
d) title()
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
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)
a) pl.pie()
b) pl.col()
c) pl.plot()
d) pl.line()
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)
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.
Library – matplotlib
interface – pyplot
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.
Runs in Overs 10 20
MI 110 224
RCB 85 210
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 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.
[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’)
Ex. pp.plot(x,y,marker=’D’,markersize=4)
Ex. pp.plot(x,y,marker=’D’,markeredgecolor=’blue’)
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()
ps_rating=[3.9,4.5,4.6,4.2,4.3]
plt.xlabel("Apps")
plt._____________("Rating") #Statement 4
plt._________ #Statement 5
plt.________ #Statement 6
ii) Write the function name and label name as displayed in the output for #statement
2 and #statement 3 respectively.
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
iii) ylabel
She has written the following code but not getting the desired output. Help her by
correcting her code.
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.ytitle("Classes") #Statement 4
plt.show()
ii) What is the correct function name for Statement 3 and Statement 4?
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.
Ans.:
1. Statement 1: plt.plot(classes,no_of_boys)
2. Statement 2:plt.plot(classes,no_of_girls)
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’)
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')
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]
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.
b) The filename for saving the plot must be written with a complete path including an
extension
Ans.:
In this section of important programs plotting with python class 12, we will cover
practicals to plot a single line chart.
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.
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.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.
import matplotlib.pyplot as pp
mon =['January','February','March','April','May','June']
views = [2500,2100,1700,3500,3000,3800]
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
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,\
pp.plot(mon,san,label='Mask',color='b',linestyle='dashed', linewidth=4,\
pp.plot(mon,hw,label='Mask',color='r',linestyle='dashed', linewidth=4,\
marker='v', markerfacecolor='k', markeredgecolor='b')
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,\
pp.legend()
pp.subplot(2,1,2)
pp.plot(mon,hw,label='Handwash',color='r',linestyle='dashed', linewidth=4,\
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.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:
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