YADU PUBLIC SCHOOL
GRADE -XII
SUBJECT – INFORMATICS PRACTICES
Chapter-4 Plotting Data using Matplotlib
Introduction
Representing large or complex data through plain text or tables is often difficult to interpret. Graphs
enhance data readability and provide clearer insights, aiding in effective decision-making. Previously,
we explored data manipulation using NumPy and Pandas. In this chapter, we will learn how to
visually represent data using various types of graphs.
Matplotlib Libraty:-
Matplotlib library of python is used to plot various 2-dimensional graphs based on data provided. The
Matplotlib library contains a variety of functions for plotting graphs.
Pyplot Module:-
Pyplot is a module of the Matplotlib library which contains functions used to plot graphs. The plot( )
function of the pyplot module is used to draw figures. To Draw figure using the plot( ) function of the
pyplot module of Matplotlib library, first we need to import thi in our program using the following
import statement.
import matplotlib.pyplot as plt
In the above import statement plt is an alias name for matplotlib.pyplot. Using this we can access all
the functions defined in the pyplot module of matplotlib library.
Elements of Graph
Plotting using plot( ) function:-
To plot X versus Y we can use plot( ) function-
Syntax:- plt.plot(X,Y)
Let’s assume that tax paid by a shopkeeper for last 4 years are as follows –
Year 2015 2016 2017 2018 2019
Tax Paid 30000 35000 33000 29000 36000
To represent this information using graph we use plot function as follows :-
Example:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30000,35000,33000,31000,36000]
plt.plot(years,tax)
Output:-
Graph Years versus Tax paid by shopkeeper.
A figure plotted using pyplot have following elements :-
● (a) grid:- Grids are used to plot grid lines on a plotted area. Syntax: plt.grid( )
● (b) legend:- Legends visual elements used to distinguish different groups of data in a graph.
plt.plot(years,tax,label=”Shop-1″)
plt.plot(years,tax2,label=”Shop-2″)
plt.legend( )
● (c) title:- title is used to specify the heading of the graph figure.
plt.title(“Data of tax paid by two shopkeers”)
● (d) xlabel:- xlabel specify label for x-axis.
plt.xlabel(“-Years-“)
● (e) ylabel:- ylabel specify label for y-axis.
plt.ylabel(“-Tax paid in Rupees-“)
● (f) yticks:-Yticks are used to set the current tick location for the y-axis.
plt.yticks([25000,30000,35000,40000,45000,50000])
""" Program to demonstrate plot( ) function and its elements """
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30756,35654,33345,31354,36453]
tax2=[31000,32000,33000,35000,54000]
yt=[25000,30000,35000,40000,45000,50000]
plt.xticks([2015,2016,2017,2018,2019])
plt.grid()
plt.plot(years,tax,label="Shop-1plplt.plot(years,tax2,label="Shop-2t.legend()
plt.title("Dataax paid by two shopkeers")
plt.xlabel("-Years-")
plt.ylabel("-Tax paid in Rupees-")
Graph:- tax paid by two shopkeeper and time with different attributes of plot function
Other arguments of plot( ) function:-
We can customize a graph plotted on plot area as per our requirement. Following customization can
be done – Changing line style, Changing line color, setting marker, changing marker style etc.
(a) Set line color:- We can change line color using color code or color name in plot( ) function as
follows-
Syntax:-
plt.plot(x-corrdinte,y-coordinaet,"color_nameExample:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30756,35654,33345,31354,36453]
plt.plot(years,tax,"blue
Output:-
Line color blue in graph
We can specify color name either through the complete name like “red”,”green”,”blue” or through
shortcut symbols like ‘r’ for “red”, ‘g’ for “green”, ‘b’ for “Blue” etc.
(b) Set line width:- We can change line width in graph using linewidth argument in plot( ) function.
line width is specified by points lime 2,4,6 bigger value means thick line.
Syntax:-
plt.plot(x-coordinate,y-coordinate,linewidth=points)
Example:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30756,35654,33345,31354,36453]
plt.plot(years,tax, linewidth=6)
Output:-
Line width in graph
(c) Set line style:- We can change line style of graph using linestyle argument in plot( ) function.
Linestyle has following values- ‘solid’, ‘dashed’, ‘dashdot’, ‘dotted’.
Syntax:-
plt.plot(x-corrdinte,y-coordinaet,linestyle='solid'/'dashed'/'dotted'/'dashdot
Example:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30756,35654,33345,31354,36453]
plt.plot(years,tax, linestyle='dashed')
Output:-
Line style in graph
(d) Set Marker Type:- Marker is the specific symbol used to represent each point on graph as per
the values of X and Y coordinate on it. We can change marker type using marker argument in plot
function.
Syntax:-
plt.plot(x-corrdinte,y-coordinaet,linestyle='valid_markerExample:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30756,35654,33345,31354,36453]
plt.plot(years,tax, marker='*')
Output:-
Marker in graph
Marker Description
“.” point
“, “ pixel
“o” circle
“v” triangle_down
“^” triangle_up
“<“ triangle_left
“>” triangle_right
“1” tri_down
“2” tri_up
“3” tri_left
“4” tri_right
“8” octagon
“s” square
“p” pentagon
“P” plus (filled)
“*” star
“h” hexagon1
“H” hexagon2
“+” plus
“x” x
Marker and Description
(e) Set Marker size:- Markersize argument in plot( ) function is used to change the size of marker in
graph.
Syntax:-
plt.plot(x-corrdinte,y-coordinaet,linestyle='valid_markerarkersize=point_value)
Example:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30756,35654,33345,31354,36453]
plt.plot(years,tax, marker='*',markersize=9)
Output:-
Marker size in plot( ) function
(f) Set Marker Color:- To change marker color we use markeredgecolor argument in plot( )
function.
Syntax:-
plt.plot(x-corrdinte,y-coordinaet,linestyle='valid_markerarkeredgecolor='colorname')
Example:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[30756,35654,33345,31354,36453]
plt.plot(years,tax, marker='*',markersize=9, markeredgecolor='red')
Output:-
Marker color in Graph
Plotting with bar( ) function:-
● Bar graph (bar chart) are used to represent data through bars of different heights.
● Bar graphs are more informative when used to compare data values.
● Bar graphs can be drawn vertically along y axis or horizontally along x-axis.
Syntax:-
plt.bar(x-corrdinte,y-coordinaet)
Example:-
import matplotlib.pyplot as plt
years=[2015,2016,2017,2018,2019]
tax=[28000,10000,33345,25000,36453]
plt.bar(years,tax)
Output:-
Bar Graph using plt.bar( ) function
Pandas Plot Function:-
The Series and dataFrame of pandas library have their own plot( ) function to draw graphs in python.
We can call plot( ) function through DataFrame or Series objects of pandas.
We can also use plt.plot( ) function of matplotlib.pyplot module with pandas plot( ) function.
Plotting Bar Graph with DataFrame:-
Syntax:-
DataFrame_object.plot(kind="graph_typeExample:-
import pandas as pd
list1=[34,54,99,54]
dic1={"Sachin":list1}
df1=pd.DataFrame(dic1,index=("Match-1","Match-2","Match-3","Match-4"))
print(df1)
df1.plot(kind="bar")
Output:-
Plotting graph using DataFrame
Syntax:- DataFrame_object.plot(kind=”graph_type”)
In above syntax the argument kind accepts a variety of graph_types to draw different graphs. Values
for kind argument are as follows:-
kind= Plot type
line plot a line in graph (Defalut)
bar plot vertical bars in graph.
hist plot histograms in graph
Values for kind argument of plot( ) function
Example:-
import matplotlib.pyplot as plt
import pandas as pd
list1=[34,54,99,54]
list2=[55,49,45,1]
list3=[10,5,11,100]
dic1={"Sachin":list1,"Virat":list2,"Dhoni":list3}
df1=pd.DataFrame(dic1, index=("Match-1","Match-2","Match-3","Match-4"))
print(df1)
df1.plot(kind="bar")
plt.title("Comparisionuns scored by players")
plt.xlabel("Matches played during las month")
plt.ylabel("Runs scored by players")
Output:-
Customizing Bar graph:-
We can customize bar graphs by passing different arguments to the plot function of DataFrame. We
can change the color of the bar, line width, line style etc.
(a) Changing color of each bar:- We can change color of each bar using color argument in plot( )
function of DataFrame. Different colors are passed as a list.
Syntax:-
DataFrame_Object.plot(kind="barlor=[color1,color2,color3])
Example:-
import pandas as pd
list1=[34,54,99,54]
list2=[55,49,45,1]
list3=[10,5,11,100]
dic1={"Sachin":list1,"Virat":list2,"Dhoni":list3}
df1=pd.DataFrame(dic1, index=("Match-1","Match-2","Match-3","Match-4"))
df1.plot(kind="bar", color=["Red","Green","Blue"])
Output:-
Bar Graph color argument
(b) Changing Edge Color:- We can change the edge color of each bar using the Edgecolor
argument of plot( ) function of DataFrame.
Syntax:-
DataFrame_Object.plot(kind="bargecolor="color_name")
Example:-
import pandas as pd
list1=[34,54,99,54]
list2=[55,49,45,1]
list3=[10,5,11,100]
dic1={"Sachin":list1,"Virat":list2,"Dhoni":list3}
df1=pd.DataFrame(dic1, index=("Match-1","Match-2","Match-3","Match-4"))
df1.plot(kind="bar", edgecolor="red")
Output:-
Edgecolor argument of plot( ) function
(c) Linewidth:- We can change edge width using the linewidth argument of plot( ) function of
DataFrame.
Syntax:-
DataFrame_Object.plot(kind="bargecolor="color_name", linewidth=points)
Example:-
import pandas as pd
list1=[34,54,99,54]
list2=[55,49,45,1]
list3=[10,5,11,100]
dic1={"Sachin":list1,"Virat":list2,"Dhoni":list3}
df1=pd.DataFrame(dic1, index=("Match-1","Match-2","Match-3","Match-4"))
df1.plot(kind="bar", edgecolor="red",linewidth=3)
Output:-
linewidth argument of plot( ) function.
(d) Changing line Style of edge of bar:- We can change line style of edge of a bar graph using the
linestyle argument of plot( ) function of DataFrame.
Syntax:-
DataFrame_Object.plot(kind="bargecolor="color_name", linestyle="Dashed/dotted/solid/dashdot")
Example:-
import pandas as pd
list1=[34,54,99,54]
list2=[55,49,45,1]
list3=[10,5,11,100]
dic1={"Sachin":list1,"Virat":list2,"Dhoni":list3}
df1=pd.DataFrame(dic1, index=("Match-1","Match-2","Match-3","Match-4"))
df1.plot(kind="bar", edgecolor="red",linewidth=3, linestyle="dashed")
Output:-
Line style argument of plot( ) function
Creating Bar Graph using .CSV file:-
We can create a bar graph using data stored in .CSV file. First we store data from a .CSV file to a
DataFrame and then use plot( ) function with the DataFrame object.
Example:-
import pandas as pd
df1=pd.read_csv("Myfile.csv", index_col="City")
print(df1)
df1.plot(kind="bar")
Output:-
Bar Graph from .CSV file
Plotting a line chart with DataFrame:-
Line charts are used to analyze growth or decline with respect to time intervals. To create a line chart
we use kind=”line” . Line chart is the default kind for plot( ) function.
Syntax:-
DataFrame_Object.plot(kind="line)
Example:-
import matplotlib.pyplot as plt
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
dic1={"Patient":list1,}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot()
plt.title("Plateletst of patient during dengue")
plt.xlabel("Days")
plt.ylabel("Platelets count in Dengue")
Output:-
Line Graph show platelets count of Dengue patient
Customizing Line graph:-
We can customize line plot drawn using DataFrame to make graph more informative.
(a) Color:- To change color of line drawn in line plot we use argument color in plot function of
DataFrame.
Syntax:-
DataFrame_object.plot(kind="linelor="color_name")
Example:-
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
dic1={"Patient":list1,}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot(color="red")
Output:-
Argument color to change color of line.
We can change the color of multiple lines plotted in a single graph using a list of colors in the color
argument used in the plot( ) function of DataFrame.
Syntax:-
DataFrame_object.plot(kind="linelor=[c1,c2,c3])
Example:-
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
list2=[260000,200000,15000, 3000,35000,90000,120000,200000]
list3=[350000,250000,150000, 80000, 150000, 250000,300000,350000]
dic1={"Patient-1":list1,"Patient-2":list2,"Patient-3":list3}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot(color=["red","green","blue"])
Output:-
line graph with multiple color
(b) Marker:- We can set marker of different type with line graph using following syntax:-
Syntax:-
DataFrame_object.plot(kind="linerker="marker_type")
Example:-
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
dic1={"Patient-1":list1}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot(marker="*")
Output:-
Marker in line graph using DataFrame
(c) Marker color:-
We can change marker color using the markeredgecolor argument in plot( ) function.
Syntax:-
DataFrame_object.plot(kind="linerker="marker_type", markeredgecolor="color_name")
Example:-
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
dic1={"Patient-1":list1}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot(marker="*",markeredgecolor="red")
Output:-
Marker color in line graph through DataFrame
(d) Marker Size:- We can change marker size using markersize argument in plot( ) function of
DataFrame. Marker size is specified in points
Syntax:-
DataFrame_object.plot(kind="linerkersize="points")
Example:-
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
dic1={"Patient-1":list1}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot(marker="*",markersize=30)
Output:-
Size of marker using markersize argument
(e) Line style:- Line style of line plot can be changed using linestyle argument of plot( ) function of
DataFrame. There are four different line styles supported by linestyle argument. (a) solid (default)
(b) dashed (c) dotted (d) dashdot
Syntax:-
DataFrame_object.plot(linestyle="solid/dashed/dotted/dashdotExample:-
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
dic1={"Patient-1":list1}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot(linestyle="dashdot")
Output:-
Line style in plot( ) function of DataFrame
(f) Line width:- We can change line width of a line plot using linewidth argument of plot( ) function
of DataFram. Here we need to specify line width in points.
Syntax:-
DataFrame_object.plot(linewidth=points)
Example:-
import pandas as pd
list1=[250000,150000,70000,35000,95000,135000,250000,300000]
dic1={"Patient-1":list1}
df1=pd.DataFrame(dic1, index=("Day-1","Day-2","Day-3","Day-4","Day-5","Day-6","Day-7","Day-8"))
print(df1)
df1.plot(linewidth=20)
Output:-
Line width argument of plot( ) function of DataFrame
Plotting a Histogram with DataFrame:-
A Histogram is used to display the grouped frequency distribution. It is used to show how many
times each value appeared in a data set or data group. Here area of rectangles standing on x-axis is
proportional to corresponding frequency.
Syntax:-
DataFrame_object.plot(kind="histExample:-
import pandas as pd
data = {'Name':['Arnav', 'Sheela', 'Azhar', 'Bincy', 'Yash',
'Nazar'],
'Weight' : [47,89,52,58,50,47]}
df=pd.DataFrame(data)
df.plot(kind='histOutput:-
Histogram using DataFrame
Customizing Histogram:-
(a) Edgecolor:- We can change edge color of rectangle drawn in histogram using edgecolor
argument of plot( ) function-
Syntax:-
DataFrame_object.plot(kind="histgecolor="color_name")
Example:-
import pandas as pd
data = {'Name':['Arnav', 'Sheela', 'Azhar', 'Bincy', 'Yash',
'Nazar'],
'Weight' : [47,89,52,58,50,47]}
df=pd.DataFrame(data)
df.plot(kind='histdgecolor="orange")
Output:-
Edgecolor argument of histogram usign DataFrame.
(b) Changing Line width of Histogram:- We can change the width of edge line of histogram using
the linewidth argument of plot( ) function.
Syntax:-
DataFrame_object.plot(kind="histgecolor="color_name", linewidth=5)
Exmple:-
import pandas as pd
data = {'Name':['Arnav', 'Sheela', 'Azhar', 'Bincy', 'Yash','Nazar'],
'Weight' : [47,89,52,58,50,47]}
df=pd.DataFrame(data)
df.plot(kind='histdgecolor="orange", linewidth=5)
Output:-
Linewidth argument of plot( ) function.
(c) Changing line style of histogram:- We can change line style of histogram using linestyle
argument of plot( ) function:-
Synatax:-
DataFrame_object.plot(kind="histgecolor="color_name", linestyle="dashed/dotted/solid/dashdot)
Example:-
import pandas as pd
data = {'Name':['Arnav', 'Sheela', 'Azhar', 'Bincy', 'Yash','Nazar'],
'Weight' : [47,89,52,58,50,47]}
df=pd.DataFrame(data)
df.plot(kind='histdgecolor="orange", linewidth=5, linestyle="dashed")
Output:-
Linestyle argument of plot( ) function