0% found this document useful (0 votes)
3 views111 pages

L3_plotting With Pyplot

Uploaded by

Yash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views111 pages

L3_plotting With Pyplot

Uploaded by

Yash
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 111

L3- Plotting with PyPlot

Lets understand :
• What is data visualization?
• Why data visualization?
• What is Matplotlip?
• Types of Matplotlip
Why do we need data visualization?
-Data visualization is the act of taking information (data)
and placing it into a visual context, such as a map or
graph.

-Data visualizations make big and small data easier for the
human brain to understand, and visualization also makes it
easier to detect patterns, trends, and outliers in groups of
data.

-Good data visualizations should place meaning into


complicated datasets so that their message is clear and
What is Data Visualization?
• Data Visualization basically refers to the graphical or visual
representation of information and data using visual elements like
charts, graphs, and maps etc.
• Data visualization unveils(to remove) patterns, trends, outliers,
correlations etc. in the data, and thereby helps decision makers
understand the meaning of data to drive business decisions.
Can u suggest me best use of data
visualization in today’s world?
• Share bazar.. where can I invest my money…
• Which is the best college… shows the pictorial presentation… in order
to say like faculties or infrastructure or campus(job) etc.
• Which areas/factors can influenced the customers.
• Areas which can be improve or attention in the organization.(Like Big
bazar/ Dmart eg)
• Yes children anything else…
Find some data visualisation tools???
• Homework….

• One of the data visualization in python is Matplotlib library’s Pyplot


interface is used.
Using Pyplot of Matplotlib Library
What is matplotlib?
• The matplotlib is a python library that provides many interfaces and
functionality for 2D graphics.
• Matplotlib is a high quality plotting library of python.
• It provides both a very quick way of visualize data from python and
publication-quality figures in many formats.
• The matplotlib library offers many different named collections of
methods. EG: PyPlot interface is one eg.
What is pyplot?

• Pyplot is a collection of methods with matplotlib which allows


to contract 2D plots easily and interactively.
• It is also a collection of functions that make matplotlib work like
MATLAB.
• Each pyplot function makes some change to a figure: e.g.,
creates a figure, creates a plotting area in a figure, plots some
lines in a plotting area, decorates the plot with labels, etc.
Installation of matplotlib
• Dear children kindly refer to the PAGE No. 172. Topic 3.2.1 for
installation of matplotlib.
• If in case you are not able to install then get it installed.

• NOTE : the matplotlib is preinstalled with ANACONDA


Importing PyPlot
• import matplotlib.pyplot
Here, matplotlib is the package and pyplot is a module that is present in
this package.
• To access the methods from pyplot:
EG: matplotlib.pyplot.plot(X,Y) here plot() is a method in pyplot module

• import matplotlib.pyplot as pl
Here, pl is an alias/ short name given to pyplot module.
• To access the methods from pyplot:
EG: pl.plot(X,Y) here with the help of alias we can access the method
Working with plot methods
import numpy as np
import matplotlib.pyplot as pl

x=np.linspace(1,5,6)
y=np.log(x)
pl.plot(x,y)
pl.show()
Basic of simple plotting
• Open Page 174
• Read all charts and see how they look like on PG 165
Line chart using plot() function
import numpy as np
import matplotlib.pyplot as pl

a=[1,2,3,4]
b=[2,4,6,8] #b has double values of list a
c=[1,4,9,16] #c has squares of list a
pl.plot(a,b)
pl.xlabel("Numbers")
pl.ylabel("Double Numbers")
pl.show()
scatter chart
import numpy as np
import matplotlib.pyplot as pl

a=[1,2,3,4]
b=[2,4,6,8] #b has double values of list a
c=[1,4,9,16] #c has squares of list a
pl.scatter(a,b) #scatter
pl.xlabel("Numbers")
pl.ylabel("Double Numbers")
pl.show()
Combination of both line and scatter
import numpy as np
import matplotlib.pyplot as pl

a=[1,2,3,4]
b=[2,4,6,8] #b has double values of list a
c=[1,4,9,16] #c has squares of list a
pl.plot(a,b) #line chart
pl.scatter(a,b) #scatter
pl.xlabel("Numbers")
pl.ylabel("Double Numbers")
pl.show()
Difference between line and scatter chart.
Line chart/line graph
1. A line chart or line graph is a type of Scatter chart/ graph
chart which displays information as a 1. Scatter plot is a graph of two sets of data
series of data points called ‘markers’ along the two axes. It is used to visualize
connected by straight line segments. the relationship between the two
variables.
2. Line graphs are usually used to find
relationship between two data sets on 2. The points are randomly distributed with
different axis; for instance X, Y. no pattern.

3. With pyplot, a line chart is created using 3. With pyplot, a scatter chart is created
plot() function. using scatter() function.

4. EG: 4. EG:
import matpotlib.pyplot.plt import matpotlib.pyplot.plt
x = [2, 4, 6] x = [2, 4, 6]
y = [1, 3, 5] y = [1, 3, 5]
plt.plot(x, y) plt. scatter(x, y)
plt.show() plt.show()
#EG2 PG179 Marks is a list that stores marks of a student in 10 units
tests. WAP to plot the student's performance in these 10 unit test.
import matplotlib.pyplot as plt NOTE:
matplotlib.pyplot.figure(figsize=(<width>,<length>))
week = [1,2,3,4,5,6,7,8,9,10] or
marks = [12,10,10,15,17,25,12,22,35,40] plt.figure(figsize =(<width>,<length>))
plt.figure(figsize=(15,7))
Will show the plot with specified size. EG
plt.grid(True)
plt.plot(week,marks) plt.figure(figsize=(15,7))
plt.xlabel('WEEK') Here, 15 units wide i.e. x-coordinate and
plt.ylabel('UNIT TEST MARKS') 7 units long i.e. y-coordinate.
plt.title('student performance')
plt.grid(True) will show grid on plot
plt.show()
Changing Line Color, Width and Style
matplotlib.pyplot.plot(X,Y,<color code>)

Commands which take color arguments can use several formats to


specify the colors. For the basic built-in colors, you can use a single
letter
b: blue g: green r: red y: yellow
m: magenta c: cyan k: black w: white

In addition to the above you can also include the names like : red,
light green, sky blue, dark grey, brown etc. , hex string (‘#008800’ )

Gray shades can be given as a string encoding a float in the 0-1 range,
Changing line Width/Thickness
matplotlib.pyplot.plot(X,Y, linewidth=<width>)
An additional parameter to plot( ) to specify the width value in points.
Line width/thickness is measured in points EG. 0.56, 3.14 etc
Changing line style
matplotlib.pyplot.plot(X,Y, linestyle=‘solid’|‘dashed’|‘dashdot’|‘dotted’)
OR
matplotlib.pyplot.plot(X,Y, ls=‘solid’|‘dashed’|‘dashdot’|‘dotted’)
OR
matplotlib.pyplot.plot(X,Y, linestyle=‘-’|‘--’|‘-.’|‘:’)
OR
matplotlib.pyplot.plot(X,Y, ls=‘-’|‘--’|‘-.’|‘:’)

An additional parameter to plot( ) to specify the linestyle


WAP having list of yr from 1960 to 2010, population of Indian and Pakistan. Show the
line chart with x-axis yr and title/label as 'countries’.
y-axis shows the population of both countries and title/label 'population in million'
pak pop displays green color and Indian orange. Graph title as 'Indo-Pak Population till
2010’
import matplotlib.pyplot as plt

year = [1960, 1970, 1980, 1990, 2000, 2010]


pop_pakistan = [44.91, 58.09, 78.07, 107.7, 138.5, 170.6]
pop_india = [449.48, 553.57, 696.783, 870.133, 1000.4, 1309.1]
plt.plot(year, pop_pakistan, color='g', linewidth=3.5, linestyle='dashed')
plt.plot(year, pop_india, color='orange', linewidth=8, ls=':’)
plt.xlabel('Countries')
plt.ylabel('Population in million')
plt.title('Pakistan India Population till 2010')
plt.show()
EG: from pg 180
import matplotlib.pyplot as plt
import numpy as np

x=np.arange(0.,10,0.1)
a=np.cos(x)
b=np.sin(x)
plt.figure(figsize=(25,10)) #25 is width and 10 ht
plt.plot(x,a, 'b',ls=':') #'b' is blue color
plt.plot(x,b, 'r’) # ‘r’ is red color

plt.show()

np.arange() is an inbuilt nd fun that returns an ndarray object containing


evenly spaced values within a defined interval
EG3 PAGE181,182
import matplotlib.pyplot as plt
import numpy as np

ar2=[1,7,21,35,35,21,7,1]
#calculating sin() cos() tan() values
s2=np.sin(ar2)
c2=np.cos(ar2)
t2=np.tan(ar2)

plt.figure(figsize=(15,7)) #figure size specified 15units width and 7 units length


plt.plot(ar2,s2, 'c') #sine line, 'c' is cyan color
plt.plot(ar2,c2,'r') #cosine line, 'r' is red color
plt.plot(ar2,t2,'k',linestyle="dashed") #tan line with black color
plt.xlabel('Array Values')
plt.ylabel('Sine,Cosine & Tangent Values')
plt.show()
Changing marker Type, Size and Color
• The data points being plotted on graph/chart are called markers.
• Syntax:
plt.plot(X,Y, ‘Color’, marker=<marker_type>, markersize=<in_points>,
markeredgecolor=<color_code>)
• There are many markers types:
Program
#Line Marker program1
import matplotlib.pyplot as plt
import numpy as np

ar2=[1,7,21,35,35,21,7,1]
#calculating sin() cos() tan() values
s2=np.sin(ar2)
c2=np.cos(ar2)
t2=np.tan(ar2)
plt.plot(ar2,s2, color=‘c') #sine line, 'c' is cyan color
plt.plot(ar2,c2,'r') #cosine line, 'r' is red color
plt.plot(ar2,t2,'k',marker='d',markersize=6, markeredgecolor='b') marker =‘d’ ie diamond
plt.xlabel('Array Values') & markersize is 6 &
color is blue
plt.ylabel('Sine,Cosine & Tangent Values')
plt.show()
#Linestyle is solid and r+ is markertype with color code are same

import matplotlib.pyplot as plt


import numpy as np

ar2=[1,7,21,35,35,21,7,1]
#calculating sin() values
s2=np.sin(ar2)

plt.plot(ar2,s2,'r+',linestyle='solid', markersize=10)

plt.xlabel('Array Values')
plt.ylabel('Sine,Cosine & Tangent Values')

plt.show()
#Linestyle is solid and r+ is markertype with color code green
import matplotlib.pyplot as plt
import numpy as np

ar2=[1,7,21,35,35,21,7,1]
#calculating sin() values
s2=np.sin(ar2)
plt.plot(ar2,s2,'r+',linestyle='solid',markeredgecolor='g')

plt.xlabel('Array Values')
plt.ylabel('Sine,Cosine & Tangent Values')

plt.show()
import matplotlib.pyplot as plt
import numpy as np
ar2=[1,7,21,35,35,21,7,1]
#calculating sin() values
s2=np.sin(ar2)
plt.plot(ar2,s2,'go’)

# ‘go’ g is for green color and ‘o’ is big circle i.e. scatter
plt.xlabel('Array Values')
plt.ylabel('Sine,Cosine & Tangent Values')
plt.show()
[NOTE: if linestyle argument is not specified than will NOT show the line
but will only display the markers]
Programs practical work
• 1st 10 terms of Fibonacci series EG4 PG184
• EG5 PG 185
Creating scatter charts
• The scatter chart is a graph of plotted points on two axes that show
the relationship between two sets of data.
• The scatter charts can be created through two functions of pyplot
library:
1) plot( ) function
2) scatter( ) function
Creating scatter charts using plot( ) function
plotting scatter chart using plot( )
import matplotlib.pyplot as plt
import numpy as np

ar2=[1,7,21,35,35,21,7,1]
#calculating sin() values
s2=np.sin(ar2)

plt.plot(ar2,s2,'o')
# ‘‘o’ is big circle i.e. scatter default color is blue

plt.xlabel('Array Values')
plt.ylabel('Sine Values')

plt.show()
plotting scatter chart using plot( )
import matplotlib.pyplot as plt
import numpy as np

ar2=[1,7,21,35,35,21,7,1]
#calculating sin() values
s2=np.sin(ar2)

plt.plot(ar2,s2,'<', markersize=20)
#'<' is for triangle and observe the markersize

plt.xlabel('Array Values')
plt.ylabel('Sine Values')
plt.show()
Programs practical work
• EG 6 PG176,177
• EG 7 PG 177
Creating scatter charts using scatter( ) function
• scatter( ) function is another function to create scatter chart of pyplot
library.
• Syntax :
matplotlib.pyplot.scatter(<array1>,<array2>)
OR
plt.scatter(<array1>,<array2>)
OR
plt.scatter(X,Y, s=markersize, c=markercolor, marker=‘markerstyle’)
Argument X,Y are arrays.
Creating scatter charts using scatter( ) function
#plotting scatter chart using scatter( )
import matplotlib.pyplot as plt
import numpy as np
ar2=[1,7,21,35,35,21,7,1]
#calculating sin() values
s2=np.sin(ar2)

plt.scatter(ar2,s2,marker='X')
#'X' is for big plus and observe

plt.xlabel('Array Values')
plt.ylabel('Sine Values')

plt.show()
scatter ( ) with all arguments
#plotting scatter chart using scatter( ) all arguments
import matplotlib.pyplot as plt
import numpy as np

ar1=np.linspace(-1,1,5)
ar2=np.exp(ar1)
colarr=['r','b','m','g','k']
sarr=[20,60,100,45,25]
plt.scatter(ar1,ar2,c=colarr,s=sarr)

plt.xlabel('Array Values')
plt.ylabel('EXP')

plt.show()
NOTE: ar1 &ar2 has 5 values. 5 colors and 5 different sizes
Programs practical work
• EG 8 PG 180
• EG 9 PG 181
• EG 10 PG 181
Creating Bar Charts
• A bar chart or bar graph is a chart or graph that presents categorical
data with rectangular bars with heights or lengths proportional to the
values that they represent. The bars can be plotted vertically or
horizontally.
• bar() function is used to create bar chart.
• Syntax:
plt.bar(X,Y,width=<width_val/sequence>,
color=<col_code/sequence>)
X,Y are sequence type.
Width scalar or array. Width(s) of the bars default 0.8. sequence
means different width to different bars.
Color scalar or array. Common color or different colors to different
bars.
#Program1
import matplotlib.pyplot as plt
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
plt.bar(langs,students)
plt.xlabel('language')
plt.ylabel('No. of Students')
plt.show()
#Program2
import matplotlib.pyplot as plt
subs=['IP','MS','GEO','MATHS']
Cl_12A=[25,50,65,50]
Cl_12B=[20,10,35,45]
plt.figure(figsize=(15,7))
plt.bar(subs,Cl_12A)
plt.bar(subs,Cl_12B)

plt.xlabel('subs')
plt.ylabel('No. of Students')
plt.legend(['12A','12B'])
plt.show()
Changing width of the bar in Bar chart
• Width means thickness of bars of bar chart.
• Default width unit is 0.8 units. Means all bars have same width.
• To specify the different width of bar plot use width argument in bar()
function.
• Width values are floats.
• Default width is 0.8 units but it can be less/more then 0.8units
• Width can be change in 2 following ways:
1) By specifying a different width for all bars of bar chart.
2) By specifying a different width for different bars of bar chart i.e. using
tuple/list sequence.
1) By specifying a different width for all bars of bar chart.

import matplotlib.pyplot as plt


#Program3 width

langs = ['C', 'C++', 'Java', 'Python', 'PHP']


students = [23,17,35,29,12]
plt.bar(langs,students,width=0.9)
# By specifying a different width for all bars of bar chart
plt.xlabel('language')
plt.ylabel('No. of Students')
plt.show()
2) By specifying a different width for different bars of bar chart i.e. using
tuple/list sequence.

import matplotlib.pyplot as plt

#Program3
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
plt.bar(langs,students,width=[0.5,0.6,0.8,0.9,0.6])
plt.xlabel('language')
plt.ylabel('No. of Students')
Width of all bars are
plt.show() different
Points to remember about widths of graphs
• To specify different widths for different bars use width argument
having sequence such as lists/tuple containing widths for each bar in
the bar() function.
• The widths given in the sequence are applied from left to right.
Means first width value of data to 1st sequence and so on..
• The width values are floats.
• Number of width values must match to the bars else python throws
an error. ValueError:shape mismatch error.
Programs practical work
• EG 11 PG 193
• EG 12 PG 194
• EG 13 PG 196
Changing color of the bar in Bar chart
• Default, a bar chart draws bars with same default color.
• Color code must be valid names.
• Colors can be change in 2 following ways:
1) By specifying a different color for all bars of bar chart i.e. one common color
for all bars.
2) By specifying a different colors for different bars of bar chart i.e. using
tuple/list sequence.
1) By specifying a different color for all bars of bar chart i.e. one common
color for all bars.
import matplotlib.pyplot as plt
#Program3
#common color
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]
plt.bar(langs,students,width=0.3, color=‘k’)
plt.xlabel('language')
plt.ylabel('No. of Students')
plt.show()
2) By specifying a different colors for different bars of bar chart i.e.
using tuple/list sequence.
import matplotlib.pyplot as plt

#Program
#different color for different bars

langs = ['C', 'C++', 'Java', 'Python', 'PHP']


students = [23,17,35,29,12]
plt.bar(langs,students,color=['r','g','b','g','y'])
plt.xlabel('language')
plt.ylabel('No. of Students')
plt.show()
Points to remember about color of graphs
• To specify different colors for different bars use color argument
having sequence such as lists/tuple containing color code for each bar
in the bar() function.
• The color code given in the sequence are applied from left to right.
Means first width value of data to 1st sequence and so on..
• The color code values are valid color codes or names.
• Number of color code values must match to the bars else python
throws an error. ValueError:shape mismatch error.
Programs practical work
• EG 14 PG 198
Creating Multiple Bars chart
• When comparing several quantities and when changing one
variable, we might want a bar chart where we have bars of one
color for one quantity value.
• We can plot multiple bar charts by playing with the thickness
and the positions of the bars. 3 different lists are created. Study
them and understand. The code will show 4 bars of 2 each i.e.
boys and girls. The bars will have a thickness of 0.25 units.
Each bar chart will be shifted 0.25 units from the previous one.
The data object has 3 lists one containing boards and other 2
for no of boys and girls appearing for board.
import matplotlib.pyplot as plt
import numpy as np

board=['CBSE','ICSE','HSC','IB']
boys=[500,300,207,250]
girls=[400,200,205,280]
br=np.arange(len(board)) #[0,1,2,3]
br1=br+0.25 #[0.25,1.25,2.25,3.25]

plt.bar(br,boys,color='r',width=0.25)
plt.bar(br1,girls,color='g',width=0.25)
plt.legend()
plt.show()
EG15 PAGE199
import matplotlib.pyplot as plt
import numpy as np

val=[[5.,25.,45.,20,],[4.,23.,49.,17.],[6.,22.,47.,19.]]
x=np.arange(4)
plt.bar(x+0.00,val[0],color='b',width=0.25)
plt.bar(x+0.25,val[1],color='g',width=0.25)
plt.bar(x+0.50,val[2],color='r',width=0.25)
plt.show()
Program
EG16 PG 200
Creating Horizontal Bar Chart
To create horizontal bar chart barh() function is used.
import matplotlib.pyplot as plt
import numpy as np

#program 1
board=['CBSE','ICSE','HSC','IB']
boys=[500,300,207,250]
girls=[400,200,205,280]
plt.barh(board,boys, color='gold')
plt.xlabel('No of Stud')
plt.ylabel('BOARD')
plt.show()
#program 2 stacked
import matplotlib.pyplot as plt
import numpy as np

board=['CBSE','ICSE','HSC','IB']
boys=[500,300,207,250]
girls=[400,200,205,230]
plt.barh(board,boys, color='gold')
plt.barh(board,girls, color='silver')
plt.xlabel('No of Stud')
plt.ylabel('BOARD')
plt.show()
#program 3
import matplotlib.pyplot as plt
import numpy as np

board=['CBSE','ICSE','HSC','IB']
boys=[500,300,207,250]
girls=[400,200,205,280]
br=np.arange(len(board)) #[0,1,2,3]`
w=0.25
plt.barh(br,boys, color='gold',height=0.25)
plt.barh(br+0.25,girls, color='silver',height=0.25)
plt.xlabel('No of Stud')
plt.ylabel('BOARD')

plt.yticks(br,board) #adds the titles to x coordinate


plt.show()
• Programs for practical
• EG 17 PAGE 201

• Plot horizontal bar char with the given following data.


• Names of animals on y-axis and speed and lifespan on x-axis with
color brown for speed and cyan for lifespan.
name= *'snail', 'pig', 'elephant', 'rabbit', 'giraffe', ‘wolf', 'horse'+
speed = [0.1, 17.5, 40, 48, 52, 69, 88]
lifespan = [2, 8, 70, 1.5, 25, 12, 28]
Customizing the plot: means giving more
specifications of graph in following ways:
1. Anatomy of a chart: means various parts of the plot:
Figure: Pyplot by default plots every chart into an area called Figure. A figsize argument
is used to set the size of the plot.
plt.figure(figsize=(width,length))
2. Axes: The axis define the area i.e. rectangular shape on which actual plot
will appear. Axes have properties like label, limits and tick marks on them.
There are two axes in a plot:
i) X-axis, the horizontal axis ii)Y-axis, the vertical axis
Axes label: means name for an axis. It is individually defined for X-axis and y-axis.
Limits: these define the range of values and number of values marked on X-axis and Y-axis.
3. Title: text that appears on the top of plot i.e. defines the chart.
4. Legends: the different colors that identify different sets of data plotted on
the plot. It is shown in a corner of the plot.
Adding a title to a plot Title

matplotlib.pyplot.title(<String title>)
OR
Plt.title(<String title>)

Y-axis Label

X-axis Label
Setting X and Y Labels, Limits and Ticks
matplotlib.pyplot. xlabel(<String title>)
OR
plt.xlabel(<String title>)

matplotlib.pyplot. ylabel(<String title>)


OR
plt.ylabel(<String title>)
Setting Xlimits and Ylimits
• Pyplot automatically defines the best fitting range for X-axis and Y-axis
depending on the data being plot.
• xlim() and ylim() functions are used to set the customized limits for X-axis
and Y-axis.
matplotlib.pyplot.xlim(<xmin>,<xmax>)
OR
plt.xlim(<xmin>,<xmax>)

matplotlib.pyplot.ylim(<ymin>,<ymax>)
OR
plt.ylim(<ymin>,<ymax>)
Inclusive of min and max values
import matplotlib.pyplot as plt
import numpy as np
#Program1

X=np.arange(4) #[0,1,2,3]
Y=[5.,25.,45.,20.]
plt.xlim(-2.0,4.0)
plt.bar(X,Y)
plt.title("TITLE")
plt.show()
• NOTE: the data that falls into the limits of X and Y axes will be plotted
means rest of the data will not shown in the plot. If X and Y axes limits
are not compatible with data values then incomplete plot is visible.
import matplotlib.pyplot as plt
import numpy as np
X=np.arange(4) #[0,1,2,3]
Y=[5.,25.,45.,20.]
plt.xlim(-4.0,1.0)
plt.bar(X,Y)
plt.title("TITLE")
plt.show()
• Practical work
• EG 21 PAGE 211
Setting Ticks for Axes
• Pyplot automatically decides which data points will have ticks on the
axes.
• To set the customized X-axis and Y-axis ticks; xticks() and yticks()
functions are used.

matplotlib.pyplot.xticks(<sequenceOfDataPoints>,[tickLabelsequence])
OR
plt.xticks(<sequenceOfDataPoints>,[tickLabelsequence])
matplotlib.pyplot.yticks(<sequenceOfDataPoints>,[tickLabelsequence])
OR
plt.yticks(<sequenceOfDataPoints>,[tickLabelsequence])
import matplotlib.pyplot as plt

#Program1

q=range(4) #[0,1,2,3]
s=[23.4,25,26,8.5]
plt.xticks([0,1,2,3])
plt.bar(q,s)
plt.show()

Customized Xticks
import matplotlib.pyplot as plt
#Program2

q=range(4,8) #[4,5,6,7]
s=[23.4,25,26,8.5]

plt.xticks([0,1,2,3])
plt.bar(q,s)
plt.show()

#will not customize the ticks as it isn't match with q val


import matplotlib.pyplot as plt
#Program2
q=range(4,8) #[4,5,6,7]
s=[23.4,25,26,8.5]

plt.xticks([5,6,7,8])
plt.bar(q,s)
plt.show()

#xtick will not appear on data point 4 as it isn’t matching


import matplotlib.pyplot as plt
q=range(4,8) #[4,5,6,7]

s=[23.4,25,26,8.5]

plt.xticks([4,5,6,7])

plt.bar(q,s)
plt.show()
#Program 3
import matplotlib.pyplot as plt
q=range(5) #[0,1,2,3,4]
langs = ['C', 'C++', 'Java', 'Python', 'PHP']
students = [23,17,35,29,12]

#plt.xticks([0,1,2,3]) #will work


#plt.xticks(langs,[1,2,3,4,5]) #gives ERROR as conversion of valuesError
plt.xticks(q,langs)
plt.bar(q,students)

plt.xlabel('language')
plt.ylabel('No. of Students')
plt.title("Students for each langunge")
plt.show()
• Programs for Practical Work
• EG 22 PG 213
• EG 23 PG 213,214
• EG24 PG 214
• EG25 PG 215
ylim(min,max)
Adding Legends
• When multiple bar chart(bar())/ multiple horizontal bar(barh()) char or line
chart(plot()) is plotted then which color is for what value can be specified
through legend.
• For adding legend legend() function is used.
matplotlib.pyplot.legend(loc=<position no./string >)
OR
plt.legend(loc=<position no./string >)
• loc signifies the position i.e. 1,2,3,4 or ‘upper right’, ‘upper left’, ‘lower left’,
‘lower right’ respectively.
• Default is 1 or ‘upper right’.
import matplotlib.pyplot as plt
import numpy as np

board=['CBSE','ICSE','HSC','IB']
boys=[500,300,207,250]
girls=[400,200,205,280]

br=np.arange(len(board)) #[0,1,2,3]
br1=br+0.25 #[0.25,1.25,2.25,3.25]
plt.bar(br,boys,color='r',width=0.1)
plt.bar(br1,girls,color='g',width=0.1)
plt.legend(['boys','girls'],loc=3)
plt.xticks(br+0.125,board)
plt.show()
• Programs for Practical Work
• Solved EG PG216
• EG 26 PG216
Saving a figure
• For saving chart savfig() function is used. It can be saved in popular
formats like .pdf, .png, .eps etc.
• Syntax:
matplotlib.pyplot.savefig(<string filename with path>)
OR
plt. savefig(<string filename with path>)
EG: plt.savefig(“BARCART.pdf”) will save plot in current directory
plt.savefig(“C:\\charts\\BARCART.pdf”) will save plot in specified directory
#Program 1
import matplotlib.pyplot as plt
import numpy as np
board=['CBSE','ICSE','HSC','IB']
boys=[500,300,207,250]
girls=[400,200,205,280]

br=np.arange(len(board)) #[0,1,2,3]
br1=br+0.25 #[0.25,1.25,2.25,3.25]
plt.bar(br,boys,color='r',width=0.1)
plt.bar(br1,girls,color='g',width=0.1)
plt.legend(board,loc=3)
plt.xticks(br+0.125,board)
plt.show()
plt.savefig("BARCHART.pdf")
Creating Histogram with Pyplot
• A histogram is an accurate representation of the distribution of
numerical data. It is an estimate of the probability distribution of
a continuous variable. It is a kind of bar graph.
• Histogram needs only single dimension array i.e. x axis and y axis
contains the frequency on values.
• A histogram is a summarisation tool for discrete or continuous data.
• To construct a histogram, follow these steps −
Bins the range of values.
Divide the entire range of values into a series of intervals.
Count how many values fall into each interval.
• The bins are usually specified as consecutive, non-overlapping
intervals of a variable.
• The matplotlib.pyplot.hist() function plots a histogram.
• Syntax:
matplotlib.pyplot.hist(x, bins=None, cumulative=False,
histtype=‘bar’, align =‘mid’, orientation=‘vertical’)
x- array or sequence of arrays
bins - integer or sequence. It is optional. If integer given, bins+1
bin-edges are calculated and returned. Default value is
automatically provided i.e. internally. In short bins divide the
entire range of values into a series of intervals and then count
how many values fall into each interval. EG
cumulative- has Boolean value. Its optional. If True, then a histogram is
computed where each bin gives the counts in that bin plus all bins for
smaller values. The last bin gives the total number of datapoints. Default
is False.
histtype: The type of histogram to draw. Default is ‘bar’.
• ‘bar’ is a traditional bar-type histogram. If multiple data are given the
bars are arranged side by side.
• ‘barstacked’ is a bar-type histogram where multiple data are stacked on
top of each other.
• ‘step’ generates a lineplot that is by default unfilled.
• ‘stepfilled’ generates a lineplot that is by default filled.
align : This parameter is an optional parameter and it controls how the
histogram is plotted. ‘left’, ‘mid’, ‘right’
orientation: this parameter is optional. It is used to plot the ‘horizontal’ or
‘vertical’ graph. If ‘horizontal’, barh() function will be used
import matplotlib.pyplot as plt
#Program1
students = [23,17,35,29,12,24,45,56,44,44]
plt.hist(students,bins=5)

plt.xlabel('language')
plt.ylabel('No. of Students')
plt.show()

Increase/decrease the bins and check


WAP to plots a histogram of marks obtained by students in a class. Four
bins, 0-25, 26-50, 51-75, and 76-100 are defined. Show the Histogram
shows number of students falling in this range.
Marks=[22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]

import matplotlib.pyplot as plt


Marks=[22,87,5,43,56,73,55,54,11,20,51,5,79,31,27]
plt.hist(Marks, bins = [0,25,50,75,100])
plt.title("histogram of result")
plt.xticks([0,25,50,75,100])
plt.xlabel('marks')
plt.ylabel('no. of students')
plt.show()
Practical work
• EG 27 page 212
• EG 28 page 212
• EG 29 page 213
randn(5) generates 5 random +ve and –ve nos.
randint(start,end-1, tot_nos) generates integers as specified in parameter.
np.randint(start,size=(row,[col]) generates nos till start-1 in specified row& column
import matplotlib.pyplot as plt
import numpy as np

y=np.random.randn(1000)

#plt.hist(y)
#plt.hist(y,bins=25)
#plt.hist(y,bins=50)
#plt.hist(y,bins=25,edgecolor="red") #border color red
#plt.hist(y,bins=25,cumulative=True)
#plt.hist(y,bins=25, histtype='step’) #will plot a steps of graph
plt.hist(y,bins=25, color='gold',histtype='step’) #graph steps color gold
plt.hist(y,bins=25, color='gold’) #graph color gold/yellow

plt.show()
import matplotlib.pyplot as plt
import numpy as np

y=np.random.randn(1000)

x=np.random.randn(1000)

plt.hist([x,y],bins=25,cumulative=True, histtype='barstacked')
plt.show()
import matplotlib.pyplot as plt
import numpy as np

y=np.random.randn(1000)
x=np.random.randn(1000)
plt.hist(x,bins=25,cumulative=True, histtype='stepfilled’)
plt.show()
import matplotlib.pyplot as plt
import numpy as np

y=np.random.randn(1000)
x=np.random.randn(1000)
plt.hist(y,bins=50, orientation='horizontal')
plt.show()
import matplotlib.pyplot as plt
import numpy as np

y=np.random.randn(1000)
x=np.random.randn(1000)
plt.hist(y,bins=25,align='right',color='grey') #align='right'/'mid'/'left’
plt.show()
Extra practical questions
1. Create an array of integers of 30 nos between 5 and 35. Draw histogram
having 10 bins and red color.
2. Create an array of integers of 30 nos between 5 and 35. Draw
cumulative histogram having 10 bins and red color.
3. Create an array of integers of 30 nos between 5 and 35. Draw
cumulative histogram having 10 bins and showing histogram border.
[HINT: histtype=‘step’+
4. Create an array of integers of 30 nos between 1 and 50. Create another
array of 30 integers between 5 and 35. Draw histogram showing the
histogram of both arrays, in the same plot. The histogram must be
cumulative True barstacked.
5. Change the orientation of above histogram to horizontal.
Plotting Data from a Dataframe

import matplotlib.pyplot as plt


import pandas as pd
#program1 plotting line graph using df
L=[['RAM', 100],['Sita',99],['Ravan',78]]
Df=pd.DataFrame(L, columns=['name','marks'], index=[1,2,3])
print(Df)
plt.plot(Df.marks)
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
#program2 plotting multiple line graph using df and reading csv file
Data=pd.read_csv("DF1.csv")
print(Data)
print(Data.Age)
plt.plot(Data.Age)
plt.plot(Data.Pojects)
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
#program 3plotting bar chart

Data=pd.read_csv("DF1.csv")
print(Data)
df_new=Data[['Age','Pojects','Budget']]
print(df_new)
plt.bar(df_new.index,df_new.Age) #single bar graph
Plt.show()
import matplotlib.pyplot as plt
import pandas as pd
#program 3plotting multiple bar chart

Data=pd.read_csv("DF1.csv")
print(Data)

df_new=Data[['Age','Pojects','Budget']]
print(df_new)
plt.bar(df_new.index,df_new.Age, color='r', width=0.5)
plt.bar(df_new.index+0.5,df_new.Pojects, color='g', width=0.5)
plt.xticks(df_new.index+0.125,Data.Name)

plt.show()
Plotting a DF’s Data using DF’s plot()
• Pandas provide a function plot() which you can use with DF as:
<DF>.plot()
This function will plot the data of the DF automatically i.e. it takes all
numeric data to plot the graph.
DF’s plot() uses the kind parameter for plotting the kind of graph.
kind=‘line’ / ‘bar’ / ‘barh’ / ‘hist’
It is an optional parameter. If no value specified than by default line
graph is plotted.
import matplotlib.pyplot as plt
import pandas as pd

#program1 plotting graph using df and reading csv file

Data=pd.read_csv("DF1.csv")
print(Data)
#Data.plot() #default is line
#Data.plot(kind='line')
#Data.plot(kind='bar')
#Data.plot(kind='barh')
Data.plot(kind='hist')
plt.show()
import matplotlib.pyplot as plt
import pandas as pd
#program2 plotting graph using df and reading csv file

Data=pd.read_csv("DF1.csv")
print(Data)
x=Data.loc[0:3,'Age':'Pojects']
print(x)
x.plot() #default is line
plt.show()

Note type(x) is <class 'pandas.core.frame.DataFrame'>


• End of chap
PIE/ circle chart or circular statistical graph
• Pie chart is divided into slices to illustrate numerical proportion.
• It is used to show parts to the whole, and often a % share.It is type of
graph in which a circle is divided into sectors that each represent a
proportion of the whole.
• Pie charts show the size of items in one data series, proportional to
the sum of the items. The data points in a pie chart are shown as a
percentage of the whole pie.
• Pyplot offers pie() function to create pie chart.
• pie() function, plots a single data range only i.e. A Pie Chart can only
display one series of data.
• The default shape of pie is oval but can be change to circle stating
axis( ) function and passing argument as “equal”
matplotlib.pyplot.axis(“equal”)
import matplotlib.pyplot as plt
import numpy as np
#program 1
subs = ['IP', 'MS', 'GEO', 'MATH']
stud = [90,40,32,250]
plt.pie(stud)
plt.show()
import matplotlib.pyplot as plt
import numpy as np

subs = ['IP', 'MS', 'GEO', 'MATH']


stud = [90,40,32,250]
plt.pie(stud, labels=subs) #Lables given to each slice
plt.title("No of students for each sub") #title of chart
plt.show()
#program 3 adding formatted %
import matplotlib.pyplot as plt
import numpy as np
subs = ['IP', 'MS', 'GEO', 'MATH']
stud = [90,40,32,250]
plt.pie(stud, labels=subs, autopct='%1.2F%%')
plt.title("No of students for each sub")
#title of chart
plt.show()
90,40,32,250 = 412
(90/412 )* 100=21.84%
(40/412) *100=9.71% and so on…
autopct explained in next slide
autopct : A string, used to label the slice with their numeric value.
The label will be placed inside the slice. The format string will be :
‘%[flag][width][.precision]type’

You might also like