0% found this document useful (0 votes)
28 views103 pages

Matplotseabornfinal

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

Matplotseabornfinal

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

Matplotlib

Topics covered:
Data visualization :
introduction to matplotlib,
line plot,
multiple subplots in one figure,
bar chart,
histogram,
box and whisker plot,
scatter plot,
pie charts,
introduction to seaborn,
seaborn Vs matplotlib,
data visualization using seaborn
Pyplot

• Most of the Matplotlib utilities lies under the pyplot submodule, and
are usually imported under the plt alias:

import [Link] as plt


Draw a line in a diagram from
position (0,0) to position (6,250):
• import [Link] as plt
import numpy as np

xpoints = [Link]([0, 6])


ypoints = [Link]([0, 250])

[Link](xpoints, ypoints)
[Link]()
Plotting x and y points

• The plot() function is used to draw points (markers) in a diagram.

• By default, the plot() function draws a line from point to point.

• The function takes parameters for specifying points in the diagram.

• Parameter 1 is an array containing the points on the x-axis.

• Parameter 2 is an array containing the points on the y-axis.

• If we need to plot a line from (1, 3) to (8, 10), we have to pass two arrays [1, 8] and [3, 10] to
the plot function.
Example
Draw a line in a diagram from position (1, 3) to
position (8, 10):
import [Link] as plt
import numpy as np

xpoints = [Link]([1, 8])


ypoints = [Link]([3, 10])

[Link](xpoints, ypoints)
[Link]()
Plotting Without Line
• To plot only the markers, you can use shortcut string
notation parameter 'o', which means 'rings'.
Example
Draw two points in the diagram, one at position (1, 3)
and one in position (8, 10):

import [Link] as pl
t
import numpy as np

xpoints = [Link]([1, 8])


ypoints = [Link]([3, 10])

[Link](xpoints,
ypoints, 'o')
[Link]()
Multiple Points

• You can plot as many points as you like, just make sure
you have the same number of points in both axis.
Example
Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and finally to
position (8, 10):

• import [Link] as plt


import numpy as np

xpoints = [Link]([1, 2, 6, 8])


ypoints =
[Link]([3, 8, 1, 10])

[Link](xpoints, ypoints)
[Link]()
Matplotlib Markers

• You can use the keyword argument marker to


emphasize each point with a specified marker:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, marker = 'o')


[Link]()
Marker Description
'o' Circle
'*' Star
'.' Point
',' Pixel
'x' X
'X' X (filled)
'+' Plus
'P' Plus (filled)
's' Square
'D' Diamond
'd' Diamond (thin)
'p' Pentagon
'H' Hexagon
'h' Hexagon
'v' Triangle Down
'^' Triangle Up
Marker Reference

'<' Triangle Left


'>' Triangle Right
'1' Tri Down
'2' Tri Up
'3' Tri Left
'4' Tri Right
'|' Vline
'_' Hline
You can use also use the shortcut string notation parameter to specify
the marker.

This parameter is also called fmt, and is written with this syntax:
marker |line | color
• Mark each point with a circle:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, 'o:r')
[Link]()
Line Reference

Line Syntax Description


'-' Solid line
':' Dotted line
'--' Dashed line
'-.' Dashed/dotted line
Color Reference

Color Syntax Description


'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Marker Size

• You can use the keyword argument markersize


or the shorter version, ms to set the size of the
markers:

Set the size of the markers to 20:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, marker = 'o', ms = 20)


[Link]()
Marker Color

• You can use the keyword argument markeredgecolor or the


shorter mec to set the color of the edge of the markers:

Set the EDGE color to red:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, marker = 'o', ms = 20, mec = 'r')


[Link]()
Face Color
• You can use the keyword argument markerfacecolor
or the shorter mfc to set the color inside the edge of
the markers:

Set the FACE color to red:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, marker = 'o', ms = 20, mfc = 'r')


[Link]()
Use both the mec and mfc arguments to color of the entire
marker:

Set the color of both the edge and the face to red:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, marker = 'o', ms = 20, mec = 'r', mfc = 'r')


[Link]()
Matplotlib Markers

You can use the keyword argument


marker to emphasize each point with a
specified marker:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, marker = 'o')


[Link]()
Marker Reference
Marker Description 'D' Diamond '3' Tri Left
'o' Circle 'd' Diamond (thin) '4' Tri Right
'*' Star 'p' Pentagon '|' Vline
'.' Point 'H' Hexagon '_' Hline
',' Pixel 'h' Hexagon
'x' X 'v' Triangle Down
'X' X (filled) '^' Triangle Up
'+' Plus '<' Triangle Left
'P' Plus (filled) '>' Triangle Right
's' Square '1' Tri Down
'2' Tri Up
Format Strings fmt
You can use also use the shortcut string notation parameter to specify
the marker.

This parameter is also called fmt, and is written with this syntax:

marker|line|color
Example

Mark each point with a circle:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, 'o:r')
[Link]()
Line Reference

Line Syntax Description


'-’ Solid line
':’ Dotted line
'--’ Dashed line
'-.’ Dashed/dotted line
Color Reference

Color Syntax Description


'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
Marker Size
You can use the keyword argument markersize or the
shorter version, ms to set the size of the markers:

Set the size of the markers to 20:

import [Link] as plt


import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, marker = 'o', ms = 20)


[Link]()
Matplotlib Line
Linestyle

• You can use the keyword argument linestyle, or shorter ls,


to change the style of the plotted line:
Example
Use a dotted line:
import [Link] as plt
import numpy as np

ypoints = [Link]([3, 8, 1, 10])

[Link](ypoints, linestyle = 'dotted')


[Link]()
Line Styles

Style Or
'solid’ (default) '-'
'dotted’ ':'
'dashed’ '--'
'dashdot’ '-.'
'None' '' or ' '
Line Color

• You can use the keyword argument color or


the shorter c to set the color of the line:

Example
Set the line color to red:

import [Link] as plt


import numpy as np
ypoints = [Link]([3, 8, 1, 10])
[Link](ypoints, color = 'r')
[Link]()
Line Width

You can use the keyword argument linewidth or


the shorter lw to change the width of the line.

The value is a floating number, in points:

Example
Plot with a 20.5pt wide line:
import [Link] as plt
import numpy as np
ypoints = [Link]([3, 8, 1, 10])
[Link](ypoints, linewidth = '20.5')
[Link]()
Multiple Lines

You can plot as many lines as you like by simply adding


more [Link]() functions:

Draw two lines by specifying a [Link]() function for each


line:

import [Link] as plt


import numpy as np
y1 = [Link]([3, 8, 1, 10])
y2 = [Link]([6, 2, 7, 11])
[Link](y1)
[Link](y2)
[Link]()
Matplotlib Labels and Title
• Create Labels for a Plot
• With Pyplot, you can use the xlabel() and ylabel() functions to
set a label for the x- and y-axis.

import numpy as np
import [Link] as plt

x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

[Link](x, y)
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link]()
Create a Title for a Plot
With Pyplot, you can use the title() function to set a title for the plot.

Example
Add a plot title and labels for the x- and y-axis:
import numpy as np
import [Link] as plt

x =
[Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125]
)
y =
[Link]([240, 250, 260, 270, 280, 290, 300, 310, 320,
330])

[Link](x, y)

[Link]("Sports Watch Data")


[Link]("Average Pulse")
[Link]("Calorie Burnage")

[Link]()
Set Font Properties for Title and
Labels
• Example
• Set font properties for the title and labels:
• import numpy as np
import [Link] as plt

x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

font1 = {'family':'serif','color':'blue','size':20}
font2 = {'family':'serif','color':'darkred','size':15}

[Link]("Sports Watch Data", fontdict = font1)


[Link]("Average Pulse", fontdict = font2)
[Link]("Calorie Burnage", fontdict = font2)

[Link](x, y)
[Link]()
Position the Title
• You can use the loc parameter in title() to position the title.
Legal values are: 'left', 'right', and 'center'. Default value is 'center'.

• Position the title to the left:

import numpy as np
import [Link] as plt
x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120,
125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310,
320, 330])
[Link]("Sports Watch Data", loc = 'left')
[Link]("Average Pulse")
[Link]("Calorie Burnage")
[Link](x, y)
Matplotlib Adding Grid Lines

• With Pyplot, you can use the grid() function to add grid lines to the plot.
import numpy as np
import [Link] as plt

x = [Link]([80, 85, 90, 95, 100, 105, 110, 115, 120, 125])
y = [Link]([240, 250, 260, 270, 280, 290, 300, 310, 320, 330])

[Link]("Sports Watch Data")


[Link]("Average Pulse")
[Link]("Calorie Burnage")

[Link](x, y)

[Link]()

[Link]()
The subplot() Function

• The subplot() function takes three arguments that describes the


layout of the figure.

• The layout is organized in rows and columns, which are represented


by the first and second argument.

• The third argument represents the index of the current plot.


Matplotlib Subplot

• With the subplot() function you can draw multiple plots in one figure:
import [Link] as plt
import numpy as np

#plot 1:
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](1, 2, 1)
[Link](x,y)

#plot 2:
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](1, 2, 2)
[Link](x,y)

[Link]()
import [Link] as plt
import numpy as np

#plot 1:
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](2, 1, 1)
[Link](x,y)

#plot 2:
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](2, 1, 2)
[Link](x,y)

[Link]()
import [Link] as plt
import numpy as np

x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](2, 3, 1)
[Link](x,y)

x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](2, 3, 2)
[Link](x,y)

x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](2, 3, 3)
[Link](x,y)

x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](2, 3, 4)
[Link](x,y)

x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](2, 3, 5)
[Link](x,y)

x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](2, 3, 6)
[Link](x,y)

[Link]()
Title

• You can add a title to each plot with the title() function:
import [Link] as plt
import numpy as np

#plot 1:
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](1, 2, 1)
[Link](x,y)
[Link]("SALES")

#plot 2:
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](1, 2, 2)
[Link](x,y)
[Link]("INCOME")

[Link]()
import [Link] as plt
import numpy as np

#plot 1:
x = [Link]([0, 1, 2, 3])
y = [Link]([3, 8, 1, 10])

[Link](1, 2, 1)
[Link](x,y)
[Link]("SALES")

#plot 2:
x = [Link]([0, 1, 2, 3])
y = [Link]([10, 20, 30, 40])

[Link](1, 2, 2)
[Link](x,y)
[Link]("INCOME")

[Link]("MY SHOP")
[Link]()
Matplotlib Scatter

• Creating Scatter Plots


• With Pyplot, you can use the scatter() function to draw a scatter plot.

• The scatter() function plots one dot for each observation. It needs two
arrays of the same length, one for the values of the x-axis, and one for
values on the y-axis:
• import [Link] as plt
import numpy as np

x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y =
[Link]([99,86,87,88,111,86,103,87,94,78,77,85
,86])

[Link](x, y)
[Link]()
Compare Plots
import [Link] as plt
import numpy as np

#day one, the age and speed of 13 cars:


x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
[Link](x, y)

#day two, the age and speed of 15 cars:


x = [Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y =
[Link]([100,105,84,105,90,99,90,95,94,100,79,112,91,80,8
5])
[Link](x, y)

[Link]()
Color Each Dot

import [Link] as plt


import numpy as np

x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors =
[Link](["red","green","blue","yellow","pink","black","
orange","purple","beige","brown","gray","cyan","magenta"
])

[Link](x, y, c=colors)

[Link]()
import [Link] as plt
import numpy as np

#day one, the age and speed of 13 cars:


x = [Link]([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = [Link]([99,86,87,88,111,86,103,87,94,78,77,85,86])
[Link](x,y,c='r')

#day two, the age and speed of 15 cars:


x = [Link]([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y=
[Link]([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
[Link](x,y,c='k',s=100) // c for color and s for size

[Link]()
Problem Statement:

Consider the following use case. A café sells six different types of bottled orange drinks.
The owner wants to understand the relationship between the price of the drinks and
how many of each one he sells, so he keeps track of how many of each drink he sells
every day.

You then create lists with the price and average sales per day for each of the six orange
drinks sold.

Finally, you create the scatter plot by using [Link]() with the two variables you wish
to compare as input arguments.
Customizing Markers in Scatter Plots

Changing the Size


The different orange drinks café owner sells come from different suppliers and have
different profit margins. You can show this additional information in the scatter plot
by adjusting the size of the marker.
The profit margin is given as a percentage:
profit_margin = [Link]([20, 35, 40, 20, 27.5, 15])
The size of the marker indicates the profit
margin for each product. The two orange
drinks that sell most are also the ones that
have the highest profit margin.
Color Each Dot
Matplotlib Bars

• Creating Bars
• With Pyplot, you can use the bar() function to draw bar graphs:
import [Link] as plt
import numpy as np

x = [Link](["A", "B", "C", "D"])


y = [Link]([3, 8, 1, 10])

[Link](x,y)
[Link]()
Horizontal Bars

• If you want the bars to be displayed horizontally


instead of vertically, use the barh() function:

Draw 4 horizontal bars:


import [Link] as plt
import numpy as np

x = [Link](["A", "B", "C", "D"])


y = [Link]([3, 8, 1, 10])

[Link](x, y)
[Link]()
Bar Color

• The bar() and barh() takes the keyword argument color to set
the color of the bars:

• Example
• Draw 4 red bars:
import [Link] as plt
import numpy as np

x = [Link](["A", "B", "C", "D"])


y = [Link]([3, 8, 1, 10])

[Link](x, y, color = "red")


[Link]()
Color Names

• Example
Draw 4 "hot pink" bars:
import [Link] as plt
import numpy as np

x =
[Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

[Link](x, y, color = "hotpink")


[Link]()
Color Hex

• Example
Draw 4 bars with a beautiful green
color:
import [Link] as plt
import numpy as np

x = [Link](["A", "B", "C", "D"])


y = [Link]([3, 8, 1, 10])

[Link](x, y, color = "#4CAF50")


[Link]()
Bar Width

• The bar() takes the keyword argument width


to set the width of the bars:
import [Link] as plt
import numpy as np

x =
[Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

[Link](x, y, width = 0.1)


[Link]()
Bar Height

• The barh() takes the keyword argument


height to set the height of the bars:
import [Link] as plt
import numpy as np

x =
[Link](["A", "B", "C", "D"])
y = [Link]([3, 8, 1, 10])

[Link](x, y, height = 0.1)


[Link]()
Matplotlib Histograms

• A histogram is a graph showing frequency distributions.


• It is a graph showing the number of observations within each given interval.
• We have to find frequency according to given range.
• In Matplotlib, we use the hist() function to create histograms.
For example there are group of peoples in a park. We can find the
people according to age group.
range frequency
0-10 ?
10-20 ?
20-30 ?
30-40 ?
40-50 ?
50-60 ?
60-70 ?
70-80 ?
80-90 ?
90-100 ?
• You can read from the histogram that there are approximately:
2 people from 140 to 145cm
5 people from 145 to 150cm
15 people from 151 to 156cm
31 people from 157 to 162cm
46 people from 163 to 168cm
53 people from 168 to 173cm
45 people from 173 to 178cm
28 people from 179 to 184cm
21 people from 185 to 190cm
4 people from 190 to 195cm
Create Histogram

• In Matplotlib, we use the hist() function to create histograms.

• The hist() function will use an array of numbers to create a histogram,


the array is sent into the function as an argument.

• For simplicity we use NumPy to randomly generate an array with 250


values, where the values will concentrate around 170, and the
standard deviation is 10
The hist() function will read the
array and produce a histogram:

import [Link] as plt


import numpy as np
# mean and standard deviation
x = [Link](170, 10, 250)
[Link](x)
[Link]()
Code:
from matplotlib import pyplot as plt
import numpy as np
peoples=[Link]([10,34,55,20,18,66,74,65,80,88,76,45,35,42,85,62,23,50])
age_group=[Link]([0,10,20,30,40,50,60,70,80,90])
[Link](peoples,age_group)
[Link]()
xticks: is used to get and set the current tick locations and labels of the x-axis.
yticks: is used to get and set the current tick locations and labels of the y-axis.

import numpy as np
import [Link] as plt

x = [1, 2, 3, 4]
y = [95, 38, 54, 35]
labels = ['Geeks1', 'Geeks2', 'Geeks3', 'Geeks4']

[Link](x, y)

# You can specify a rotation for the tick


# labels in degrees or with keywords.
[Link](x, labels, rotation ='vertical')
[Link](y, rotation ='vertical')

[Link]()
import numpy as np
import [Link] as plt

x = [Link]([1, 2, 3, 4])
y = [Link]([95, 38, 54, 35])
labels = [Link](['Geeks1', 'Geeks2', 'Geeks3', 'Geeks4'])

[Link](x,weights=y)

[Link]()
import numpy as np
import [Link] as plt

x = [Link]([1, 2, 3, 4])
y = [Link]([95, 38, 54, 35])
labels = [Link](['Geeks1', 'Geeks2', 'Geeks3', 'Geeks4'])

[Link](x,weights=y)
# You can specify a rotation for the tick
# labels in degrees or with keywords.
[Link](x, labels, rotation ='vertical')
[Link](y, rotation ='vertical')

[Link]()
rwidth,xticks,yticks
import [Link] as plt
peoples=[10,34,55,20,18,66,74,65,80,88,76,45,35,42,85,62,23,50]
age_group=[0,10,20,30,40,50,60,70,80,90]
[Link](peoples,age_group,rwidth=0.8)
[Link](age_group)
[Link](range(0,5))
[Link]()
Matplotlib Pie Charts
Creating Pie Charts use the pie() function
pie chart draws one piece (called a wedge) for each value in the list
By default the plotting of the first wedge starts from the x-axis and move anti
clockwise

import [Link] as plt


import numpy as np

y = [Link]([35, 25, 25, 15])

[Link](y)
[Link]()
Labels
• Add labels to the pie chart with the label parameter.
• The label parameter must be an array with one label for each wedge:
import [Link] as plt
import numpy as np

y = [Link]([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

[Link](y, labels = mylabels)


[Link]()
startangle
Start the first wedge at 90 degrees:
import [Link] as plt
import numpy as np

y = [Link]([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]

[Link](y, labels = mylabels, startangle = 90)


[Link]()
Explode

you want one of the wedges to stand out? The explode parameter allows you to do
that.
Example
Pull the "Apples" wedge 0.2 from the center of the pie:
import [Link] as plt
import numpy as np

y = [Link]([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]

[Link](y, labels = mylabels, explode = myexplode)


[Link]()
Shadow

Add a shadow to the pie chart by setting the shadows parameter to


True:
import [Link] as plt
import numpy as np

y = [Link]([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
myexplode = [0.2, 0, 0, 0]

[Link](y, labels = mylabels, explode = myexplode, shadow = True)


[Link]()
Colors

import [Link] as plt


import numpy as np

y = [Link]([35, 25, 25, 15])


mylabels = ["Apples", "Bananas", "Cherries", "Dates"]
mycolors = ["black", "hotpink", "b", "#4CAF50"]

[Link](y, labels = mylabels, colors = mycolors)


[Link]()
legend()
import [Link] as plt
import numpy as np

y = [Link]([35,25,25,15])
mylabels = ["Apples","Bananas","Cherries","Dates"]

[Link](y,labels =mylabels,startangle =190)


[Link](title="Fruits")
[Link]()
Box and whisker plot
A box plot which is also known as a whisker plot displays a summary of a set of data containing the minimum,
first quartile, median, third quartile, and maximum. In a box plot, we draw a box from the first quartile to the
third quartile. A vertical line goes through the box at the median. The whiskers go from each quartile to the
minimum or maximum.
Q1

Q4
Q2 Q3

Median=45
Range=max-min(95-15)
IQR=70-30
Boxplots can be used to:
• Identify outliers or irregular data points
• To determine if our data is skewed
• To understand the spread/range of the data
Boxplot can be drawn calling [Link]() and [Link](), or [Link]() to
visualize the distribution of values within each column.

import pandas as pd
import numpy as np
df = [Link]([Link](10, 5), columns=['A', 'B', 'C', 'D', 'E’])
[Link]()
# Import libraries
import [Link] as plt
import numpy as np

# Creating dataset
[Link](10)
data = [Link](100, 20, 200)

fig = [Link](figsize =(5, 4))

# Creating plot
[Link](data)

# show plot
[Link]()
# Import libraries
import [Link] as plt
import numpy as np Add_axes():
The parameter is used to set new
dimensions of the figure. It takes
# Creating dataset values in float also.
[Link](10)

data_1 = [Link](100, 10, 200)


data_2 = [Link](90, 20, 200)
data_3 = [Link](80, 30, 200)
data_4 = [Link](70, 40, 200)
data = [data_1, data_2, data_3, data_4]

fig = [Link](figsize =(10, 7))

# Creating axes instance


ax = fig.add_axes([0, 0, 1, 1]) //[left, bottom, width, height]

# Creating plot
bp = [Link](data)

# show plot
[Link]()
import pandas as pd
import [Link] as plt

dataframe = pd.read_csv("[Link]")

fixed_acidity = dataframe["fixed acidity"]


free_sulfur_dioxide = dataframe['free sulfur dioxide']
total_sulfur_dioxide = dataframe['total sulfur dioxide']
alcohol = dataframe['alcohol']

fig, ax = [Link]()
[Link](fixed_acidity)
[Link]()
introduction to seaborn
Seaborn is the extended version of Matplotlib which uses Matplotlib
along with Numpy and Pandas for plotting graphs.
It helps in visualizing univariate and bivariate data. It uses beautiful
themes for decorating Matplotlib graphics.
1. Univariate data –
This type of data consists of only one variable.
2. Bivariate data –
This type of data involves two different variables.
seaborn Vs matplotlib
Features Matplotlib Seaborn
Seaborn contains a number of patterns and plots for data
It is utilized for making basic graphs. Datasets are visualised with the
visualization. It uses fascinating themes. It helps in
Functionality help of bargraphs, histograms, piecharts, scatter plots, lines and so
compiling whole data into a single plot. It also provides
on.
distribution of data.
It uses comparatively simple syntax which is easier to learn
It uses comparatively complex and lengthy syntax. Example: Syntax
Syntax and understand. Example: Syntax for bargraph-
for bargraph- [Link](x_axis, y_axis).
[Link](x_axis, y_axis).
We can open and use multiple figures simultaneously. However they
are closed distinctly. Syntax to close one figure at a time: Seaborn sets time for the creation of each figure. However,
Dealing Multiple Figures
[Link](). Syntax to close all the figures: it may lead to (OOM) out of memory issues
[Link](“all”)

Matplotlib is well connected with Numpy and Pandas and acts as a


Seaborn is more comfortable in handling Pandas data
graphics package for data visualization in python. Pyplot provides
Visualization frames. It uses basic sets of methods to provide beautiful
similar features and syntax as in MATLAB. Therefore, MATLAB users
graphics in python.
can easily study it.
Seaborn avoids overlapping of plots with the help of its
Pliability Matplotlib is a highly customized and robust
default themes
Seaborn is much more functional and organized than
Matplotlib works efficiently with data frames and [Link] treats
Matplotlib and treats the whole dataset as a single unit.
Data Frames and Arrays figures and axes as objects. It contains various stateful APIs for
Seaborn is not so stateful and therefore, parameters are
plotting. Therefore plot() like methods can work without parameters.
required while calling methods like plot()
Seaborn is the extended version of Matplotlib which uses
Use Cases Matplotlib plots various graphs using Pandas and Numpy Matplotlib along with Numpy and Pandas for plotting
graphs
data visualization using seaborn
Importing Datasets
Seaborn comes with a few important datasets in the library. When Seaborn is
installed, the datasets download automatically.

import seaborn as sns


print(sns.get_dataset_names())
import dataset using load_dataset()
import seaborn as sns
import pandas as pd
df=sns.load_dataset('planets')

print([Link])
print([Link](10))
Using pandas to read csv file and plot graph
using seaborn
import seaborn as sns
import pandas as pd
import [Link] as pl
df=pd.read_csv("D:\\aug 2023\\CA\\D2216\\CA2\\[Link]")
[Link](figsize=(5,5))
[Link](x='CA2-practical',y='CA2-viva',data=df)
[Link]()
Create Multiple line plots with HUE:
We can add multiple line plots by using the hue parameter. You can
create multiple lines by grouping variables using the hue parameter.
Suppose in fmri dataset event column
having two types: stim and cue. Then we
can plot multiline by using hue parameter
import seaborn as sns
import [Link] as pl
df=sns.load_dataset('fmri')
[Link](x='timepoint',y='signal',data=df,hue='event')

[Link]()
style and markers parameters

Style: to highlight line graph using solid line


Markers: to highlight line graph using points
import seaborn as sns
import [Link] as pl
df=sns.load_dataset('fmri')
[Link](x='timepoint',y='signal',data=df,hue='event',style='event',markers=True)

[Link]()
Plot bar graph using barplot()
import seaborn as sns
import [Link] as pl
df=sns.load_dataset('fmri')
[Link](figsize=(10,5))
[Link](x='timepoint',y='signal',data=df,hue='event')

[Link]()
Use of palette in barplot()
import seaborn as sns
import [Link] as pl
df=sns.load_dataset('fmri')
[Link](figsize=(10,5))
[Link](x='timepoint',y='signal',data=df,palette='rocket')

[Link]()
The possible value of the palette are:
scatterplot()
import seaborn as sns
import [Link] as pl
df=sns.load_dataset('fmri')
[Link](figsize=(5,5))
[Link](x='timepoint',y='signal',data=df)

[Link]()
Heatmap
Heatmap is a graphical representation of 2D (two dimensional) data.
Each data value represents in a matrix and it has a special color.
[Link]
# importing the modules
import numpy as np
import seaborn as sn
import [Link] as plt
import pandas as pd
cmap="RdBu"
df=pd.read_csv('D:\\[Link]')
df1=[Link](columns=['name'],axis=0).set_index('id')
[Link](figsize=(5,5))
[Link](df1,annot=True,cmap=cmap)
[Link]()
# importing the modules
import numpy as np
import seaborn as sn
import [Link] as plt

# generating 2-D 10x10 matrix of random numbers


# from 1 to 100
data = [Link](low = 1,
high = 100,
size = (10, 10))
print("The data to be plotted:\n")
print(data)

# plotting the heatmap


hm = [Link](data = data,annot=True)

# displaying the plotted heatmap


[Link]()

You might also like