17 Plotting With Pyplot
17 Plotting With Pyplot
LINE CHART: It is a type of chart which displays information as a series of data points
called markers connected by straight line segments.With PyPlot, a line chart is
created using plot() function.
Example of Line chart program
import matplotlib.pyplot as pl
a=[1,2,3,4,5,6]
b=[1,4,9,16,25,36]
pl.plot(a,b)
To label x-axis, the horizontal axis,as ‘Values’ and the y axis, the vertical axis as
‘Squared values’. We can set x-axis and y-axis labels using functions xlabel() and ylabel()
respectively. We can write the same as
import matplotlib.pyplot as pl
a=[1,2,3,4,5,6]
b=[1,4,9,16,25,36]
pl.xlabel(‘values’)
pl.ylabel(‘Squared values’)
pl.plot(a,b)
The output produced by the above Python program is given below
The plot() function allows us specity multiple settings for our chart/graph such as
1. Color (line color/marker color)
2. Marker size
3. Marker type
CHANGING LINE COLOR AND STYLE
LINE COLOR:
We can use color codes as :’r’ for red,’y’ for yellow, ‘g’ for green,’b’ for blue etc.
import matplotlib.pyplot as pl
a=[1,2,3,4,5,6]
b=[1,4,9,16,25,36]
pl.xlabel(‘values’)
pl.ylabel(‘Squared values’)
pl.figure(figsize(10,5))
pl,grid(True)
pl.plot(a,b,’r’)
LINE WIDTH: linewidth=<width>,
Linewidth = 2.5 where width value is given in points
import matplotlib.pyplot as pl
a=[1,2,3,4,5,6]
b=[1,4,9,16,25,36]
pl.xlabel("values")
pl.ylabel("Squared values")
pl.figure(figsize=(10,10))
pl.grid(False)
pl.plot(a,b,'r', linewidth=7.5)
LINE STYLE: linestyle or is =[ solid| dashed, dashdot,dotted
import matplotlib.pyplot as pl
a=[1,2,3,4,5,6]
b=[1,4,9,16,25,36]
pl.xlabel("values")
pl.ylabel("Squared values")
pl.figure(figsize=(10,10))
pl.grid(False)
pl.plot(a,b,'g', linestyle=”dotted”, linewidth=7.5)
import matplotlib.pyplot as pl
import numpy as np
x= np.arange(0,10,.1)
a= np.sin(x)
b= np.cos(x)
pl.plot(x,a,"b")
pl.plot(x,b,"r")
pl.show()
CHANGING MARKER TYPE, SIZE AND COLOR
import matplotlib.pyplot as pl
a=[1,2,3,4,5,6]
b=[1,4,9,16,25,36]
pl.xlabel("values")
pl.ylabel("Squared values")
pl.figure(figsize=(10,15))
pl.grid(False)
pl.plot(a,b,'rD', markersize=5,markeredgecolor="blue")
CLASS
The order of the bars plotted may be different from the order in actual data sequence
import matplotlib.pyplot as pl
Class=["I","II","III","IV","V"]
girls=[98,104,55,95,103]
boys=[44,86,98,90,97]
pl.figure(figsize=(5,5))
pl.grid(False)
pl.bar(Class, girls)
pl.bar(Class, boys)
pl.xlabel("CLASS")
pl.ylabel("GIRLS, BOYS STRENGTH")
import matplotlib.pyplot as pl
Class=["I","II","III","IV","V"]
strength=[198,204,215,230,250]
girls=[98,104,55,95,103]
boys=[44,86,98,90,97]
pl.figure(figsize=(5,5))
pl.grid(False)
pl.bar(Class, strength)
pl.bar(Class, girls)
pl.bar(Class, boys)
pl.xlabel("CLASS")
pl.ylabel("GIRLS, BOYS STRENGTH")
pl.bar(girls,boys ,width=[.2,.4,1.6,.8,1.0])
pl.xlabel("CLASS")
pl.ylabel("GIRLS, BOYS STRENGTH")
import matplotlib.pyplot as pl
Class=["I","II","III","IV","V"]
girls=[98,104,100,95,103]
boys=[44,86,58,90,97]
pl.figure(figsize=(5,5))
pl.grid(False)
pl.bar(Class,boys,color=["r","b","c","k","y"])
pl.xlabel("CLASS")
pl.ylabel("BOYS STRENGTH")
In scatter() function we use c argument for color and in bar() function we use color
argument for selecting color
import matplotlib.pyplot as pl
import numpy as np
Class=["I","II","III","IV","V"]
girls=[98,104,100,95,103]
boys=[44,86,58,90,97]
X=np.arange(5)
pl.grid(True)
pl.bar(X+0.5,boys,color="red",width=0.25)
pl.bar(X+0.75,girls,color="blue",width=0.25)
pl.xlabel("CLASS")
pl.ylabel("BOYS STRENGTH")
NOTE: We can plot using any type of sequence (e.g. lists/ tuples/ arrays/ individual rows/
columns of dataframe as data.
CREATING HORIZONTAL BAR CHART
import matplotlib.pyplot as pl
Class=["I","II","III","IV","V"]
girls=[98,104,100,95,103]
boys=[44,86,58,90,97]
pl.figure(figsize=(5,5))
pl.grid(False)
pl.barh(Class,boys,color=["r","b","c","k","y"])
pl.xlabel("CLASS")
pl.ylabel("BOYS STRENGTH")
import matplotlib.pyplot as pl
Marks=[95,97,92,89,98]
Subjects=['ENGLISH','SANSKRIT','MATHS','SCIENCE','SST']
pl.pie(Marks,labels=Subjects,autopct='%1.1F%%')
“%[flags][width][.precision]type”
% -It is a special string which will determine the format of the values to be displayed
Width -total no. of characters to be displayed digits before and after decimal point + 1 for
decimal point
Flag -When 0 is specified, 0 will be preceded before the value if the value is less than
the width
Precision -no. of digits after the decimal point
Type - d or i for integer, f or F for float
%% - to print % symbol
Marks=[95,97,92,89,98]
Subjects=['ENGLISH','SANSKRIT','MATHS','SCIENCE','SST']
pl.pie(Marks,labels=Subjects,autopct='%1.1F%%')
import matplotlib.pyplot as pl
Marks=[95,97,92,89,98]
Subjects=['ENGLISH','SANSKRIT','MATHS','SCIENCE','SST']
pl.pie(Marks,labels=Subjects,autopct='%3.1fd%%')
SAVING A FIGURE