XII IP CH 3 Plotting With Pyplot

Download as pdf or txt
Download as pdf or txt
You are on page 1of 52

Plotting with Pyplot

Data Visualization
Data visualization refers to the graphical or
visual representation of information and data
using visual elements like charts, graphs, and
maps, etc.

It is very useful in decision making and unveils


the trends, patterns, outliers, correlations, etc.
in the data.
Matplotlib
• The matplotlib is a Python library that
provides many interfaces and functionality
for 2D graphics. It is preinstalled with
Anaconda distribution.

• PyPlot is a collection of methods within


matplotlib library which allows the users
construct 2D plots easily and interactively.
Importing PyPlot
import matplotlib.pyplot

OR

import matplotlib.pyplot as pl
Line Chart
A line chart or line plot or line graph is a type of
chart which displays information as a series of
data points called 'markers' connected by
straight line segments.
Bar Chart
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.
Histogram
• A histogram is a plot that
lets you discover, and
show, the underlying
frequency distribution
(shape) of a set of
continuous data.

• Unlike the bar chart,


there are no gaps
between the bars,
although some bars might
be "absent" reflecting no
frequencies.
Line Chart
• Line charts can be created using the plot()
function of the matplotlib.pyplot module.
• You can have multiple lines in a line chart,
change color, change type of line and much
more.
import matplotlib.pyplot as pl
A=[1,2,3,4]
B=[2,4,6,8]
pl.plot(A,B)
pl.show()
import matplotlib.pyplot as pl
A=[1,2,3,4]
B=[1,4,9,16]
pl.plot(A,B)
pl.show()
Pyplot functions to customise plots
• xlabel() – To add a label on the x-axis
• ylabel() – To add a label on the y-axis
• title() – To add a heading/ title to the chart
• show() – To display the chart
• grid() – To display a grid in the background
• xticks() – To set the tick mark labels on x-axis
• yticks() – To set the tick mark labels on y-axis
• legend() – To display the legends
• savefig() – To save the chart as a pdf/ jpg
import matplotlib.pyplot as pl
A=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug']
B=[68.65,70.94,71.81,72.86,73.13,71.62,70.44,72.80]
pl.plot(A,B)
pl.ylabel("Price in Rs.")
pl.xlabel("2019")
pl.show()
Customising Line Plot – plot() function
arguments
The plot() function allows you to specify multiple settings
for your chart/graph to make it more informative.
• color
• linewidth
• linestyle / ls
• marker
• markersize
• markeredgecolor/ mec
• markerfacecolor/ mfc
Changing Line Color
import matplotlib.pyplot as pl
pl.plot(A,B,'r')
pl.xlabel("2019")
pl.ylabel("Price in Rs.")
pl.show()

'r' red
'g' green
'b' blue
'm' magenta
'y' yellow
'k' black
'c' cyan
'w' white
Changing Line Style
linestyle or ls = [ 'solid' | 'dashed' | 'dashdot' | 'dotted' ]

import matplotlib.pyplot as pl
pl.plot( A , B , 'r' , ls='dotted' )
pl.xlabel("2019")
pl.ylabel("Price in Rs.")
pl.show()
Changing Marker Type, Size and Color
marker=<value>, markersize=<points>, markeredgecolor=<color>

'.' point 's' square '3' tri left


',' pixel 'p' pentagon '4' tri right
'o' circle '*' star 'v' triangle down
'+' plus 'h' Hexagon1 '^' triangle up
'x' x 'H' Hexagon2 '<' triangle left
'D' diamond '1' tri down '>' triangle right
'd' thin diamond '2' tri up '|' vline
'_' hline
import matplotlib.pyplot as pl
pl.plot( A , B , 'r' , ls='dotted' , marker='D',
markersize=5, markeredgecolor='b')
pl.ylabel("Price in Rs.")
pl.xlabel("2019")
pl.show()
• Marker style and line color can be combined.
• If markeredgecolor is not specified separately, it
takes same color as line
pl.plot( A , B , 'r+' , ls='solid', markeredgecolor='k')
import matplotlib.pyplot as plt
Year = [1920,1930,1940,1950,1960,1970,1980,1990,2000,2010]
Unemployment_Rate = [9.8,12,8,7.2,6.9,7,6.5,6.2,5.5,6.3]

plt.plot(Year, Unemployment_Rate, color='red', marker='o')


plt.title('Unemployment Rate Vs Year', fontsize=14)
plt.xlabel('Year', fontsize=14)
plt.ylabel('Unemployment Rate', fontsize=14)
plt.grid(True)
plt.show()
Bar Chart
• Bar charts can be created using the bar() and
barh() functions of the matplotlib.pyplot
module.
Creating Bar Charts
import matplotlib.pyplot as pl
cities=['Delhi','Mumbai','Bengaluru','Kolkata']
pop=[10927986,12691836,5104047,4631392]
pl.bar(cities,pop)
pl.xlabel('Cities')
pl.ylabel('Population')
pl.show()
• The order of the bars plotted may be different
from the order in actual data sequence.

• By default, the bar chart draws bars with


equal width (0.8 units)
Customising Bar Plot – bar() function
arguments
The bar() function allows you to specify multiple settings
for your chart/graph to make it more informative.
• color
• linewidth
• linestyle / ls
• edgecolor
• hatch - pattern ( '-', '+', 'x', '\\', '*', 'o', 'O', '.')
• fill – True/False
Changing width of bars
pl.bar(x, y, width = value/sequence)
pl.bar(cities, pop, width=0.3)
pl.bar(cities, pop, width=[0.5,0.6,0.7,0.8])
Changing color of bars
pl.bar(x, y, color = code/name/sequence)
pl.bar(cities, pop, color='g')
pl.bar(cities, pop, color=['r','g','b','black'])
Creating Multiple Bars chart
import matplotlib.pyplot as pl
x=np.array([2017,2018,2019])
sc= [75.22 , 83.21 , 83.03]
comm= [75.13 , 82.54 , 78.69]
arts= [67.12 , 74.85 , 86.32]
pl.bar(x ,sc ,color='r', width=0.2)
pl.bar(x+0.2,comm,color='b', width=0.2)
pl.bar(x+0.4,arts, color='g', width=0.2)
pl.show()
Creating Horizontal Bar Chart
A=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug']
B=[68.65,70.94,71.81,72.86,73.13,71.62,70.44,
72.80]
pl.barh( A , B )
Customizing the plot
• Adding a Title
pl.title("Chart title")
• Setting Limits
pl.xlim(<xmin> , <xmax> )
pl.ylim(<ymin> , <ymax> )
• Setting Ticks
xticks(<sequence of tick points>, <tick labels>)
yticks(<sequence of tick points>, <tick labels>)
A school celebrated volunteering week where each
section of class XI dedicated a day for collecting
amount for charity being supported by the school.
Section A volunteered on Monday, B on Tuesday
and so on. There are six sections in class XI,
Amount collected by sections A to F are 8000,
12000, 9800, 11200, 15500 and 7300.
Plot the collected amount vs the days in the bar
chart.
import matplotlib.pyplot as pl
amount=[8000,12000,9800,11200,15500,7300]
pl.title("Volunteering Week Collection")
pl.bar(x , amount , color='r', width=0.25)
pl.xlabel('Days')
pl.ylabel('Collection')
pl.show()
Adding Legends
pl.legend(loc=<position number or string>)

position number position string


1 upper right
2 upper left
3 lower left
4 lower right
x=np.array([2017,2018,2019])
per=[[75.22,83.21,83.03],[75.13,82.54,78.69],[67.12,74.85,
86.32]]
pl.bar(x ,per[0],color='b', width=0.25,label='Science')
pl.bar(x+0.25,per[1],color='r', width=0.25,label='Commerce')
pl.bar(x+0.50,per[2],color='g', width=0.25,label='Arts')
pl.title("Average Percentage")
pl.xticks([2017,2018,2019])
pl.xlabel('Year')
pl.ylim(0,100)
pl.ylabel('Percentage')
pl.legend(loc='lower right')
Saving a figure
If you want to save the plot created using pyplot
functions for later use, you can use savefig() to
save the plot.

pl.savefig("chart.pdf")
pl.savefig("d:\\python\\chart.png")
Creating Histogram
• A histogram is a plot that lets you discover, and show, the
underlying frequency distribution (shape) of a set of
continuous data.

• A histogram provides a visual interpretation of numerical


data by showing the number of data points that fall within
a specified range of values (called bins).

• A histogram in Python can be created using the hist( )


function

• Unlike the bar chart, there are no gaps between the bars,
although some bars might be "absent" reflecting no
frequencies.
hist() function
• This function of pyplot is used to create histograms.

pl.hist(x, bins=None, cumulative=False, histtype='bar',


align='mid', orientation='vertical')

x array to be plotted on histogram


bins optional, integer if given, it calculates
and returns bins+1 edges.
cumulative optional, boolean, if true then each bin
gives the count in that bin plus all the
bins for smaller values, default false.
histtype optional,['bar', 'barstacked', 'step',
'stepfilled'], default bar
orientation ['horizontal', 'vertical'], optional, default
vertical
• Consider the following dataset containing ages
of 20 people.
37 20 38 44 53 69 74 53 35 38 66 46 24 46 100 48 51 62 58 57

BIN Frequency Scores included in Bin


20 – 30 2 20,24
30 – 40 4 37,38,35,38
40 – 50 4 44,46,45,48
50 – 60 5 53,53,51,58,57
60 – 70 3 69,66,62
70 – 80 1 74
80 – 90 0 ---
90 – 100 1 100
DataFrame.hist(column=None, by=None,
grid=True, bins=10)

column string/ sequence, used to limit the data


by object, used to form histograms for
seperate groups.
grid True (default)/ False
bins integer/ sequence, default 10
DF=pd.DataFrame({'Age':[37,20,38,44,53,69,74,
53,35,38,66,46,24,46,100,48,51,62,58,57]})
print(DF)
DF.hist()
x=[1,1,2,3,3,5,7,8,9,10,10,11,11,13,13,15,16,17,18,
18,18,19,20,21,21,23,24,24,25,25,25,25,26,26,26,2
7,27,27,27,27,29,30,30,31,33,34,34,34,35,36,36,37,
37,38,38,39,40,41,41,42,43,44,45,45,46,47,48,48,4
9,50,51,52,53,54,55,55,56,57,58,60,61,63,64,65,66,
68,70,71,72,74,75,77,81,83,84,87,89,90,90,91 ]
pl.hist(x, bins=10)
pl.show()
import matplotlib.pyplot as pl
import numpy as np
a=np.random.randn(1000)
pl.hist(a)
pl.hist(a,bins=25)
y=np.random.randn(1000)
pl.hist(y,25,edgecolor='r')
pl.hist(y, bins=30,cumulative=True)
pl.hist(y,bins=20,histtype='step')
pl.hist(y,bins=50, orientation='horizontal')
Plotting Data from DataFrame
import pandas as pd
import matplotlib.pyplot as pl

df=pd.DataFrame({'Name':['Vijaya','Rahul','Meghna', 'Radhika','Shourya'],
'Marks':[80,92, 67, 95,97],
'Age':[32, 28, 30, 25, 20]})
print(df)

You might also like