0% found this document useful (0 votes)
11 views110 pages

IDS U-5

Uploaded by

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

IDS U-5

Uploaded by

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

UNIT 5

Matplotlib:

Matplotlib is a low level graph plotting library in python that serves as a


visualization utility.

Matplotlib was created by John D. Hunter.

Matplotlib is open source and we can use it freely.

Matplotlib is mostly written in python, a few segments are written in C, Objective-


C and Javascript for Platform compatibility.

Where is the Matplotlib Codebase?

The source code for Matplotlib is located at this github


repository https://github.com/matplotlib/matplotlib

Installation of Matplotlib

If you have Python and PIP already installed on a system, then installation of
Matplotlib is very easy.

Install it using this command:

C:\Users\Your Name>pip install matplotlib

If this command fails, then use a python distribution that already has Matplotlib
installed, like Anaconda, Spyder etc.

Import Matplotlib
Once Matplotlib is installed, import it in your applications by adding
the import module statement:

import matplotlib

Now Matplotlib is imported and ready to use:

Checking Matplotlib Version

The version string is stored under __version__ attribute.

ExampleGet your own Python Server


import matplotlib

print(matplotlib.__version__)

2.0.0

Matplotlib Pyplot
Pyplot

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

import matplotlib.pyplot as plt

Now the Pyplot package can be referred to as plt.

ExampleGet your own Python Server

Draw a line in a diagram from position (0,0) to position (6,250):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([0, 6])


ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()

Result:

Matplotlib Plotting
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.

ExampleGet your own Python Server

Draw a line in a diagram from position (1, 3) to position (8, 10):

import matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints)
plt.show()

Result:

lotting 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 matplotlib.pyplot as plt


import numpy as np

xpoints = np.array([1, 8])


ypoints = np.array([3, 10])

plt.plot(xpoints, ypoints, 'o')


plt.show()
Result:

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 matplotlib.pyplot as plt
import numpy as np

xpoints = np.array([1, 2, 6, 8])


ypoints = np.array([3, 8, 1, 10])

plt.plot(xpoints, ypoints)
plt.show()
Result:

Default X-Points

If we do not specify the points on the x-axis, they will get the default values 0, 1, 2,
3 etc., depending on the length of the y-points.

So, if we take the same example as above, and leave out the x-points, the diagram
will look like this:

Example

Plotting without x-points:


import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10, 5, 7])

plt.plot(ypoints)
plt.show()
Result:

The x-points in the example above are [0, 1, 2, 3, 4, 5].

Matplotlib Markers
Markers

You can use the keyword argument marker to emphasize each point with a
specified marker:

ExampleGet your own Python Server

Mark each point with a circle:


import matplotlib.pyplot as plt
import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o')


plt.show()

Example

Mark each point with a star:

...
plt.plot(ypoints, marker = '*')
...
Result:
Result:

Format Strings fmt

You can 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 matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, 'o:r')
plt.show()
Result:
The marker value can be anything from the Marker Reference above.

The line value can be one of the following:

Line Reference

Line Syntax Description

'-' Solid line Try it »

':' Dotted line Try it »

'--' Dashed line Try it »

'-.' Dashed/dotted line Try it »

Note: If you leave out the line value in the fmt parameter, no line will be plotted.

The short color value can be one of the following:


Color Reference
import sys

import matplotlib

matplotlib.use('Agg')

import matplotlib.pyplot as plt

import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, 'or')

plt.show()

#Two lines to make our compiler able to draw:

plt.savefig(sys.stdout.buffer)

sys.stdout.flush()

RESULT:
Marker Size

You can use the keyword argument markersize or the shorter version, ms to set the
size of the markers:

Example

Set the size of the markers to 20:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', ms = 20)


plt.show()

Result:
Try it Yourself »

Marker Color

You can use the keyword argument markeredgecolor or the shorter mec to set the
color of the edge of the markers:

Example

Set the EDGE color to red:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r')


plt.show()

Result:
Example

Set the FACE color to red:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', ms = 20, mfc = 'r')


plt.show()

Result:
Use both the mec and mfc arguments to color the entire marker:

Example

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

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, marker = 'o', ms = 20, mec = 'r', mfc = 'r')


plt.show()

Result:
Example

Mark each point with a beautiful green color:

...
plt.plot(ypoints, marker = 'o', ms = 20, mec = '#4CAF50', mfc = '#4CAF50')
...

Result:
Example

Mark each point with the color named "hotpink":

...
plt.plot(ypoints, marker = 'o', ms = 20, mec = 'hotpink', mfc = 'hotpink')
...

Result:
Matplotlib Line
Linestyle

You can use the keyword argument linestyle, or shorter ls, to change the style of
the plotted line:

ExampleGet your own Python Server

Use a dotted line:

import matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle = 'dotted')


plt.show()

Result:
Try it Yourself »

Example

Use a dashed line:

plt.plot(ypoints, linestyle = 'dashed')

Result:
Shorter Syntax

The line style can be written in a shorter syntax:

linestyle can be written as ls.

dotted can be written as :.

dashed can be written as --.

Example

Shorter syntax:

plt.plot(ypoints, ls = ':')

Result:
Line Styles

You can choose any of these styles:

Style Or

'solid' (default) '-'

'dotted' ':'

'dashed' '--'

'dashdot' '-.'

'None'
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 matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, color = 'r')


plt.show()

Result:
Try it Yourself »

You can also use Hexadecimal color values:

Example

Plot with a beautiful green line:

...
plt.plot(ypoints, c = '#4CAF50')
...

Result:
Try it Yourself »

Or any of the 140 supported color names.

Example

Plot with the color named "hotpink":

...
plt.plot(ypoints, c = 'hotpink')
...

Result:
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 matplotlib.pyplot as plt


import numpy as np

ypoints = np.array([3, 8, 1, 10])


plt.plot(ypoints, linewidth = '20.5')
plt.show()

Result:

Multiple Lines

You can plot as many lines as you like by simply adding more plt.plot() functions:

Example

Draw two lines by specifying a plt.plot() function for each line:

import matplotlib.pyplot as plt


import numpy as np
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])

plt.plot(y1)
plt.plot(y2)

plt.show()

Result:

You can also plot many lines by adding the points for the x- and y-axis for each
line in the same plt.plot() function.

(In the examples above we only specified the points on the y-axis, meaning that the
points on the x-axis got the the default values (0, 1, 2, 3).)
The x- and y- values come in pairs:

Example

Draw two lines by specifiyng the x- and y-point values for both lines:

import matplotlib.pyplot as plt


import numpy as np

x1 = np.array([0, 1, 2, 3])
y1 = np.array([3, 8, 1, 10])
x2 = np.array([0, 1, 2, 3])
y2 = np.array([6, 2, 7, 11])

plt.plot(x1, y1, x2, y2)


plt.show()

Result:
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.

ExampleGet your own Python Server

Add labels to the x- and y-axis:


import numpy as np
import matplotlib.pyplot as plt

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

plt.plot(x, y)

plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.show()

Result:
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 matplotlib.pyplot as plt

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

plt.plot(x, y)

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.show()

Result:
Set Font Properties for Title and Labels

You can use the fontdict parameter in xlabel(), ylabel(), and title() to set font
properties for the title and labels.

Example

Set font properties for the title and labels:

import numpy as np
import matplotlib.pyplot as plt

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

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

plt.title("Sports Watch Data", fontdict = font1)


plt.xlabel("Average Pulse", fontdict = font2)
plt.ylabel("Calorie Burnage", fontdict = font2)

plt.plot(x, y)
plt.show()

Result:

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'.
Example

Position the title to the left:

import numpy as np
import matplotlib.pyplot as plt

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

plt.title("Sports Watch Data", loc = 'left')


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)
plt.show()

Result:

Matplotlib Adding Grid Lines


Add Grid Lines to a Plot
With Pyplot, you can use the grid() function to add grid lines to the plot.

ExampleGet your own Python Server

Add grid lines to the plot:

import numpy as np
import matplotlib.pyplot as plt

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

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid()

plt.show()

Result:
Try it Yourself »

ADVERTISEMENT

Specify Which Grid Lines to Display

You can use the axis parameter in the grid() function to specify which grid lines to
display.

Legal values are: 'x', 'y', and 'both'. Default value is 'both'.

Example

Display only grid lines for the x-axis:


import numpy as np
import matplotlib.pyplot as plt

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

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid(axis = 'x')

plt.show()

Result:
Try it Yourself »

Example

Display only grid lines for the y-axis:

import numpy as np
import matplotlib.pyplot as plt

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

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")
plt.plot(x, y)

plt.grid(axis = 'y')

plt.show()

Result:

Try it Yourself »

Set Line Properties for the Grid

You can also set the line properties of the grid, like this: grid(color = 'color',
linestyle = 'linestyle', linewidth = number).
Example

Set the line properties of the grid:

import numpy as np
import matplotlib.pyplot as plt

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

plt.title("Sports Watch Data")


plt.xlabel("Average Pulse")
plt.ylabel("Calorie Burnage")

plt.plot(x, y)

plt.grid(color = 'green', linestyle = '--', linewidth = 0.5)

plt.show()

Result:
Matplotlib Subplot
Display Multiple Plots

With the subplot() function you can draw multiple plots in one figure:

ExampleGet your own Python Server

Draw 2 plots:

import matplotlib.pyplot as plt


import numpy as np

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

plt.subplot(1, 2, 1)
plt.plot(x,y)

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

plt.subplot(1, 2, 2)
plt.plot(x,y)

plt.show()

Result:
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.

plt.subplot(1, 2, 1)
#the figure has 1 row, 2 columns, and this plot is the first plot.

plt.subplot(1, 2, 2)
#the figure has 1 row, 2 columns, and this plot is the second plot.
So, if we want a figure with 2 rows an 1 column (meaning that the two plots will
be displayed on top of each other instead of side-by-side), we can write the syntax
like this:

Example

Draw 2 plots on top of each other:

import matplotlib.pyplot as plt


import numpy as np

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

plt.subplot(2, 1, 1)
plt.plot(x,y)

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

plt.subplot(2, 1, 2)
plt.plot(x,y)

plt.show()

Result:
You can draw as many plots you like on one figure, just descibe the number of
rows, columns, and the index of the plot.

Example

Draw 6 plots:

import matplotlib.pyplot as plt


import numpy as np

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 1)
plt.plot(x,y)
x = np.array([0, 1, 2, 3])
y = np.array([10, 20, 30, 40])

plt.subplot(2, 3, 2)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 3)
plt.plot(x,y)

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

plt.subplot(2, 3, 4)
plt.plot(x,y)

x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])

plt.subplot(2, 3, 5)
plt.plot(x,y)

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

plt.subplot(2, 3, 6)
plt.plot(x,y)

plt.show()

Result:
ADVERTISEMENT

Title

You can add a title to each plot with the title() function:

Example

2 plots, with titles:

import matplotlib.pyplot as plt


import numpy as np

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

plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")

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

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.show()

Result:
Super Title

You can add a title to the entire figure with the suptitle() function:

Example

Add a title for the entire figure:

import matplotlib.pyplot as plt


import numpy as np

#plot 1:
x = np.array([0, 1, 2, 3])
y = np.array([3, 8, 1, 10])
plt.subplot(1, 2, 1)
plt.plot(x,y)
plt.title("SALES")

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

plt.subplot(1, 2, 2)
plt.plot(x,y)
plt.title("INCOME")

plt.suptitle("MY SHOP")
plt.show()

Result:

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:

ExampleGet your own Python Server

A simple scatter plot:

import matplotlib.pyplot as plt


import numpy as np

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

plt.scatter(x, y)
plt.show()

Result:
The observation in the example above is the result of 13 cars passing by.

The X-axis shows how old the car is.

The Y-axis shows the speed of the car when it passes.

Are there any relationships between the observations?

It seems that the newer the car, the faster it drives, but that could be a coincidence,
after all we only registered 13 cars.

Compare Plots
In the example above, there seems to be a relationship between speed and age, but
what if we plot the observations from another day as well? Will the scatter plot tell
us something else?

Example

Draw two plots on the same figure:

import matplotlib.pyplot as plt


import numpy as np

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


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

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


x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)

plt.show()

Result:
Note: The two plots are plotted with two different colors, by default blue and
orange, you will learn how to change colors later in this chapter.

By comparing the two plots, I think it is safe to say that they both gives us the
same conclusion: the newer the car, the faster it drives.

ADVERTISEMENT

Colors

You can set your own color for each scatter plot with the color or the c argument:

Example
Set your own color of the markers:

import matplotlib.pyplot as plt


import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y, color = 'hotpink')

x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y, color = '#88c999')

plt.show()

Result:
Color Each Dot

You can even set a specific color for each dot by using an array of colors as value
for the c argument:

Note: You cannot use the color argument for this, only the c argument.

Example

Set your own color of the markers:

import matplotlib.pyplot as plt


import numpy as np

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

plt.scatter(x, y, c=colors)

plt.show()

Result:
ColorMap

The Matplotlib module has a number of available colormaps.

A colormap is like a list of colors, where each color has a value that ranges from 0
to 100.

Here is an example of a colormap:


This colormap is called 'viridis' and as you can see it ranges from 0, which is a
purple color, up to 100, which is a yellow color.

How to Use the ColorMap

You can specify the colormap with the keyword argument cmap with the value of
the colormap, in this case 'viridis' which is one of the built-in colormaps available
in Matplotlib.

In addition you have to create an array with values (from 0 to 100), one value for
each point in the scatter plot:

Example

Create a color array, and specify a colormap in the scatter plot:

import matplotlib.pyplot as plt


import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.show()

Result:

You can include the colormap in the drawing by including


the plt.colorbar() statement:

Example

Include the actual colormap:

import matplotlib.pyplot as plt


import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
colors = np.array([0, 10, 20, 30, 40, 45, 50, 55, 60, 70, 80, 90, 100])

plt.scatter(x, y, c=colors, cmap='viridis')

plt.colorbar()

plt.show()

Result:

Available ColorMaps

You can choose any of the built-in colormaps:


Name Reverse

Accent Accent_r

Blues Blues_r

BrBG BrBG_r

BuGn BuGn_r

BuPu BuPu_r

CMRmap CMRmap_r

Dark2 Dark2_r

GnBu GnBu_r

Greens Greens_r

Greys Greys_r

OrRd OrRd_r
Oranges Oranges_r

PRGn PRGn_r

Paired Paired_r

Pastel1 Pastel1_r

Pastel2 Pastel2_r

PiYG PiYG_r

Size

You can change the size of the dots with the s argument.

Just like colors, make sure the array for sizes has the same length as the arrays for
the x- and y-axis:

Example

Set your own size for the markers:

import matplotlib.pyplot as plt


import numpy as np

x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])
plt.scatter(x, y, s=sizes)

plt.show()

Result:

Alpha

You can adjust the transparency of the dots with the alpha argument.

Just like colors, make sure the array for sizes has the same length as the arrays for
the x- and y-axis:

Example

Set your own size for the markers:

import matplotlib.pyplot as plt


import numpy as np
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
sizes = np.array([20,50,100,200,500,1000,60,90,10,300,600,800,75])

plt.scatter(x, y, s=sizes, alpha=0.5)

plt.show()

Result:

Combine Color Size and Alpha

You can combine a colormap with different sizes of the dots. This is best
visualized if the dots are transparent:

Example

Create random arrays with 100 values for x-points, y-points, colors and sizes:

import matplotlib.pyplot as plt


import numpy as np
x = np.random.randint(100, size=(100))
y = np.random.randint(100, size=(100))
colors = np.random.randint(100, size=(100))
sizes = 10 * np.random.randint(100, size=(100))

plt.scatter(x, y, c=colors, s=sizes, alpha=0.5, cmap='nipy_spectral')

plt.colorbar()

plt.show()

Result:

Matplotlib Bars
Creating Bars

With Pyplot, you can use the bar() function to draw bar graphs:
ExampleGet your own Python Server

Draw 4 bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x,y)
plt.show()

Result:

The bar() function takes arguments that describes the layout of the bars.

The categories and their values represented by the first and second argument as
arrays.

Example
x = ["APPLES", "BANANAS"]
y = [400, 350]
plt.bar(x, y)

Horizontal Bars

If you want the bars to be displayed horizontally instead of vertically, use


the barh() function:

Example

Draw 4 horizontal bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.barh(x, y)
plt.show()

Result:
Bar Color

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

Example

Draw 4 red bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "red")


plt.show()

Result:
Color Names

You can use any of the 140 supported color names.

Example

Draw 4 "hot pink" bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "hotpink")


plt.show()

Result:
Color Hex

Or you can use Hexadecimal color values:

Example

Draw 4 bars with a beautiful green color:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, color = "#4CAF50")


plt.show()

Result:
Bar Width

The bar() takes the keyword argument width to set the width of the bars:

Example

Draw 4 very thin bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.bar(x, y, width = 0.1)


plt.show()

Result:
The default width value is 0.8

Note: For horizontal bars, use height instead of width.

Bar Height

The barh() takes the keyword argument height to set the height of the bars:

Example

Draw 4 very thin bars:

import matplotlib.pyplot as plt


import numpy as np

x = np.array(["A", "B", "C", "D"])


y = np.array([3, 8, 1, 10])

plt.barh(x, y, height = 0.1)


plt.show()

Result:
Matplotlib Histograms
Histogram

A histogram is a graph showing frequency distributions.

It is a graph showing the number of observations within each given interval.

Example: Say you ask for the height of 250 people, you might end up with a
histogram like this:
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.
Learn more about Normal Data Distribution in our Machine Learning Tutorial.

ExampleGet your own Python Server

A Normal Data Distribution by NumPy:

import numpy as np

x = np.random.normal(170, 10, 250)

print(x)

Result:

This will generate a random result, and could look like this:

[167.62255766 175.32495609 152.84661337 165.50264047 163.17457988


162.29867872 172.83638413 168.67303667 164.57361342 180.81120541
170.57782187 167.53075749 176.15356275 176.95378312 158.4125473
187.8842668 159.03730075 166.69284332 160.73882029 152.22378865
164.01255164 163.95288674 176.58146832 173.19849526 169.40206527
166.88861903 149.90348576 148.39039643 177.90349066 166.72462233
177.44776004 170.93335636 173.26312881 174.76534435 162.28791953
166.77301551 160.53785202 170.67972019 159.11594186 165.36992993
178.38979253 171.52158489 173.32636678 159.63894401 151.95735707
175.71274153 165.00458544 164.80607211 177.50988211 149.28106703
179.43586267 181.98365273 170.98196794 179.1093176 176.91855744
168.32092784 162.33939782 165.18364866 160.52300507 174.14316386
163.01947601 172.01767945 173.33491959 169.75842718 198.04834503
192.82490521 164.54557943 206.36247244 165.47748898 195.26377975
164.37569092 156.15175531 162.15564208 179.34100362 167.22138242
147.23667125 162.86940215 167.84986671 172.99302505 166.77279814
196.6137667 159.79012341 166.5840824 170.68645637 165.62204521
174.5559345 165.0079216 187.92545129 166.86186393 179.78383824
161.0973573 167.44890343 157.38075812 151.35412246 171.3107829
162.57149341 182.49985133 163.24700057 168.72639903 169.05309467
167.19232875 161.06405208 176.87667712 165.48750185 179.68799986
158.7913483 170.22465411 182.66432721 173.5675715 176.85646836
157.31299754 174.88959677 183.78323508 174.36814558 182.55474697
180.03359793 180.53094948 161.09560099 172.29179934 161.22665588
171.88382477 159.04626132 169.43886536 163.75793589 157.73710983
174.68921523 176.19843414 167.39315397 181.17128255 174.2674597
186.05053154 177.06516302 171.78523683 166.14875436 163.31607668
174.01429569 194.98819875 169.75129209 164.25748789 180.25773528
170.44784934 157.81966006 171.33315907 174.71390637 160.55423274
163.92896899 177.29159542 168.30674234 165.42853878 176.46256226
162.61719142 166.60810831 165.83648812 184.83238352 188.99833856
161.3054697 175.30396693 175.28109026 171.54765201 162.08762813
164.53011089 189.86213299 170.83784593 163.25869004 198.68079225
166.95154328 152.03381334 152.25444225 149.75522816 161.79200594
162.13535052 183.37298831 165.40405341 155.59224806 172.68678385
179.35359654 174.19668349 163.46176882 168.26621173 162.97527574
192.80170974 151.29673582 178.65251432 163.17266558 165.11172588
183.11107905 169.69556831 166.35149789 178.74419135 166.28562032
169.96465166 178.24368042 175.3035525 170.16496554 158.80682882
187.10006553 178.90542991 171.65790645 183.19289193 168.17446717
155.84544031 177.96091745 186.28887898 187.89867406 163.26716924
169.71242393 152.9410412 158.68101969 171.12655559 178.1482624
187.45272185 173.02872935 163.8047623 169.95676819 179.36887054
157.01955088 185.58143864 170.19037101 157.221245 168.90639755
178.7045601 168.64074373 172.37416382 165.61890535 163.40873027
168.98683006 149.48186389 172.20815568 172.82947206 173.71584064
189.42642762 172.79575803 177.00005573 169.24498561 171.55576698
161.36400372 176.47928342 163.02642822 165.09656415 186.70951892
153.27990317 165.59289527 180.34566865 189.19506385 183.10723435
173.48070474 170.28701875 157.24642079 157.9096498 176.4248199 ]

Try it Yourself »

The hist() function will read the array and produce a histogram:

Example
A simple histogram:

import matplotlib.pyplot as plt


import numpy as np

x = np.random.normal(170, 10, 250)

plt.hist(x)
plt.show()

Result:

Matplotlib Pie Charts


Creating Pie Charts

With Pyplot, you can use the pie() function to draw pie charts:

ExampleGet your own Python Server

A simple pie chart:


import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])

plt.pie(y)
plt.show()

Result:

As you can see the pie chart draws one piece (called a wedge) for each value in the
array (in this case [35, 25, 25, 15]).

By default the plotting of the first wedge starts from the x-axis and
moves counterclockwise:
Note: The size of each wedge is determined by comparing the value with all the
other values, by using this formula:

The value divided by the sum of all values: x/sum(x)

Labels
Add labels to the pie chart with the label parameter.

The label parameter must be an array with one label for each wedge:

Example

A simple pie chart:


import matplotlib.pyplot as plt
import numpy as np

y = np.array([35, 25, 25, 15])


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

plt.pie(y, labels = mylabels)


plt.show()

Result:

Start Angle
As mentioned the default start angle is at the x-axis, but you can change the start
angle by specifying a startangle parameter.

The startangle parameter is defined with an angle in degrees, default angle is 0:

Example

Start the first wedge at 90 degrees:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


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

plt.pie(y, labels = mylabels, startangle = 90)


plt.show()

Result:
Explode
Maybe you want one of the wedges to stand out? The explode parameter allows
you to do that.

The explode parameter, if specified, and not None, must be an array with one value
for each wedge.

Each value represents how far from the center each wedge is displayed:

Example

Pull the "Apples" wedge 0.2 from the center of the pie:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


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

plt.pie(y, labels = mylabels, explode = myexplode)


plt.show()

Result:

Shadow
Add a shadow to the pie chart by setting the shadows parameter to True:

Example

Add a shadow:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


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

plt.pie(y, labels = mylabels, explode = myexplode, shadow = True)


plt.show()

Result:

Colors
You can set the color of each wedge with the colors parameter.

The colors parameter, if specified, must be an array with one value for each wedge:

Example

Specify a new color for each wedge:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


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

plt.pie(y, labels = mylabels, colors = mycolors)


plt.show()

Result:

You can use Hexadecimal color values, any of the 140 supported color names, or
one of these shortcuts:

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

Legend
To add a list of explanation for each wedge, use the legend() function:

Example

Add a legend:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


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

plt.pie(y, labels = mylabels)


plt.legend()
plt.show()

Result:

Legend With Header

To add a header to the legend, add the title parameter to the legend function.

Example
Add a legend with a header:

import matplotlib.pyplot as plt


import numpy as np

y = np.array([35, 25, 25, 15])


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

plt.pie(y, labels = mylabels)


plt.legend(title = "Four Fruits:")
plt.show()

Result:

Seaborn
Visualize Distributions With Seaborn
Seaborn is a library that uses Matplotlib underneath to plot graphs. It will be used
to visualize random distributions.
Install Seaborn.

If you have Python and PIP already installed on a system, install it using this
command:

C:\Users\Your Name>pip install seaborn

If you use Jupyter, install Seaborn using this command:

C:\Users\Your Name>!pip install seaborn

Distplots

Distplot stands for distribution plot, it takes as input an array and plots a curve
corresponding to the distribution of points in the array.

Import Matplotlib
Import the pyplot object of the Matplotlib module in your code using the following
statement:

import matplotlib.pyplot as plt

You can learn about the Matplotlib module in our Matplotlib Tutorial.

Import Seaborn
Import the Seaborn module in your code using the following statement:

import seaborn as sns

Plotting a Distplot
ExampleGet your own Python Server
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot([0, 1, 2, 3, 4, 5])

plt.show()
Plotting a Distplot Without the Histogram
Example
import matplotlib.pyplot as plt
import seaborn as sns

sns.distplot([0, 1, 2, 3, 4, 5], hist=False)

plt.show()

Geographic Data with Basemap


One common type of visualization in data science is that of geographic data.
Matplotlib's main tool for this type of visualization is the Basemap toolkit, which is
one of several Matplotlib toolkits which lives under the mpl_toolkits namespace.
Admittedly, Basemap feels a bit clunky to use, and often even simple
visualizations take much longer to render than you might hope. More modern
solutions such as leaflet or the Google Maps API may be a better choice for more
intensive map visualizations. Still, Basemap is a useful tool for Python users to
have in their virtual toolbelts. In this section, we'll show several examples of the
type of map visualization that is possible with this toolkit.

Installation of Basemap is straightforward; if you're using conda you can type this
and the package will be downloaded:
$ conda install basemap

We add just a single new import to our standard boilerplate:

In [1]:
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
Once you have the Basemap toolkit installed and imported, geographic plots are
just a few lines away (the graphics in the following also requires the PIL package
in Python 2, or the pillow package in Python 3):

In [2]:
plt.figure(figsize=(8, 8))
m = Basemap(projection='ortho', resolution=None, lat_0=50, lon_0=-100)
m.bluemarble(scale=0.5);
The meaning of the arguments to Basemap will be discussed momentarily.

The useful thing is that the globe shown here is not a mere image; it is a fully-
functioning Matplotlib axes that understands spherical coordinates and which
allows us to easily overplot data on the map! For example, we can use a different
map projection, zoom-in to North America and plot the location of Seattle. We'll
use an etopo image (which shows topographical features both on land and under
the ocean) as the map background:

In [3]:
fig = plt.figure(figsize=(8, 8))
m = Basemap(projection='lcc', resolution=None,
width=8E6, height=8E6,
lat_0=45, lon_0=-100,)
m.etopo(scale=0.5, alpha=0.5)

# Map (long, lat) to (x, y) for plotting


x, y = m(-122.3, 47.6)
plt.plot(x, y, 'ok', markersize=5)
plt.text(x, y, ' Seattle', fontsize=12);
This gives you a brief glimpse into the sort of geographic visualizations that are
possible with just a few lines of Python. We'll now discuss the features of Basemap
in more depth, and provide several examples of visualizing map data. Using these
brief examples as building blocks, you should be able to create nearly any map
visualization that you desire.

Map Projections
The first thing to decide when using maps is what projection to use. You're
probably familiar with the fact that it is impossible to project a spherical map, such
as that of the Earth, onto a flat surface without somehow distorting it or breaking
its continuity. These projections have been developed over the course of human
history, and there are a lot of choices! Depending on the intended use of the map
projection, there are certain map features (e.g., direction, area, distance, shape, or
other considerations) that are useful to maintain.

The Basemap package implements several dozen such projections, all referenced
by a short format code. Here we'll briefly demonstrate some of the more common
ones.

We'll start by defining a convenience routine to draw our world map along with the
longitude and latitude lines:

In [4]:
from itertools import chain

def draw_map(m, scale=0.2):


# draw a shaded-relief image
m.shadedrelief(scale=scale)
# lats and longs are returned as a dictionary
lats = m.drawparallels(np.linspace(-90, 90, 13))
lons = m.drawmeridians(np.linspace(-180, 180, 13))

# keys contain the plt.Line2D instances


lat_lines = chain(*(tup[1][0] for tup in lats.items()))
lon_lines = chain(*(tup[1][0] for tup in lons.items()))
all_lines = chain(lat_lines, lon_lines)
# cycle through these lines and set the desired style
for line in all_lines:
line.set(linestyle='-', alpha=0.3, color='w')
Cylindrical projections
The simplest of map projections are cylindrical projections, in which lines of
constant latitude and longitude are mapped to horizontal and vertical lines,
respectively. This type of mapping represents equatorial regions quite well, but
results in extreme distortions near the poles. The spacing of latitude lines varies
between different cylindrical projections, leading to different conservation
properties, and different distortion near the poles. In the following figure we show
an example of the equidistant cylindrical projection, which chooses a latitude
scaling that preserves distances along meridians. Other cylindrical projections are
the Mercator (projection='merc') and the cylindrical equal area (projection='cea')
projections.

In [5]:
fig = plt.figure(figsize=(8, 6), edgecolor='w')
m = Basemap(projection='cyl', resolution=None,
llcrnrlat=-90, urcrnrlat=90,
llcrnrlon=-180, urcrnrlon=180, )
draw_map(m)
The additional arguments to Basemap for this view specify the latitude (lat) and
longitude (lon) of the lower-left corner (llcrnr) and upper-right corner (urcrnr) for
the desired map, in units of degrees.

Pseudo-cylindrical projections
Pseudo-cylindrical projections relax the requirement that meridians (lines of
constant longitude) remain vertical; this can give better properties near the poles of
the projection. The Mollweide projection (projection='moll') is one common
example of this, in which all meridians are elliptical arcs. It is constructed so as to
preserve area across the map: though there are distortions near the poles, the area
of small patches reflects the true area. Other pseudo-cylindrical projections are the
sinusoidal (projection='sinu') and Robinson (projection='robin') projections.

In [6]:
fig = plt.figure(figsize=(8, 6), edgecolor='w')
m = Basemap(projection='moll', resolution=None,
lat_0=0, lon_0=0)
draw_map(m)
The extra arguments to Basemap here refer to the central latitude (lat_0) and
longitude (lon_0) for the desired map.

Perspective projections

Perspective projections are constructed using a particular choice of perspective


point, similar to if you photographed the Earth from a particular point in space (a
point which, for some projections, technically lies within the Earth!). One common
example is the orthographic projection (projection='ortho'), which shows one side
of the globe as seen from a viewer at a very long distance. As such, it can show
only half the globe at a time. Other perspective-based projections include the
gnomonic projection (projection='gnom') and stereographic projection
(projection='stere'). These are often the most useful for showing small portions of
the map.

Here is an example of the orthographic projection:

In [7]:
fig = plt.figure(figsize=(8, 8))
m = Basemap(projection='ortho', resolution=None,
lat_0=50, lon_0=0)
draw_map(m);
Conic projections
A Conic projection projects the map onto a single cone, which is then unrolled.
This can lead to very good local properties, but regions far from the focus point of
the cone may become very distorted. One example of this is the Lambert
Conformal Conic projection (projection='lcc'), which we saw earlier in the map of
North America. It projects the map onto a cone arranged in such a way that two
standard parallels (specified in Basemap by lat_1 and lat_2) have well-represented
distances, with scale decreasing between them and increasing outside of them.
Other useful conic projections are the equidistant conic projection
(projection='eqdc') and the Albers equal-area projection (projection='aea'). Conic
projections, like perspective projections, tend to be good choices for representing
small to medium patches of the globe.

In [8]:
fig = plt.figure(figsize=(8, 8))
m = Basemap(projection='lcc', resolution=None,
lon_0=0, lat_0=50, lat_1=45, lat_2=55,
width=1.6E7, height=1.2E7)
draw_map(m)

Other projections

If you're going to do much with map-based visualizations, I encourage you to read


up on other available projections, along with their properties, advantages, and
disadvantages. Most likely, they are available in the Basemap package. If you dig
deep enough into this topic, you'll find an incredible subculture of geo-viz geeks
who will be ready to argue fervently in support of their favorite projection for any
given application!

Drawing a Map Background

Earlier we saw the bluemarble() and shadedrelief() methods for projecting global
images on the map, as well as the drawparallels() and drawmeridians() methods for
drawing lines of constant latitude and longitude. The Basemap package contains a
range of useful functions for drawing borders of physical features like continents,
oceans, lakes, and rivers, as well as political boundaries such as countries and US
states and counties. The following are some of the available drawing functions that
you may wish to explore using IPython's help features:
 Physical boundaries and bodies of water
o drawcoastlines(): Draw continental coast lines
o drawlsmask(): Draw a mask between the land and sea, for use with
projecting images on one or the other
o drawmapboundary(): Draw the map boundary, including the fill color
for oceans.
o drawrivers(): Draw rivers on the map
o fillcontinents(): Fill the continents with a given color; optionally fill
lakes with another color
 Political boundaries
o drawcountries(): Draw country boundaries
o drawstates(): Draw US state boundaries
o drawcounties(): Draw US county boundaries
 Map features
o drawgreatcircle(): Draw a great circle between two points
o drawparallels(): Draw lines of constant latitude
o drawmeridians(): Draw lines of constant longitude
o drawmapscale(): Draw a linear scale on the map
 Whole-globe images
o bluemarble(): Project NASA's blue marble image onto the map
o shadedrelief(): Project a shaded relief image onto the map
o etopo(): Draw an etopo relief image onto the map
o warpimage(): Project a user-provided image onto the map

 = plt.subplots(1, 2, figsize=(12, 8))



 for i, res in enumerate(['l', 'h']):
 m = Basemap(projection='gnom', lat_0=57.3, lon_0=-6.2,
 width=90000, height=120000, resolution=res, ax=ax[i])
 m.fillcontinents(color="#FFDDCC", lake_color='#DDEEFF')
 m.drawmapboundary(fill_color="#DDEEFF")
 m.drawcoastlines()
 ax[i].set_title("resolution='{0}'".format(res));
ax = plt.subplots(1, 2, figsize=(12, 8))

for i, res in enumerate(['l', 'h']):


m = Basemap(projection='gnom', lat_0=57.3, lon_0=-6.2,
width=90000, height=120000, resolution=res, ax=ax[i])
m.fillcontinents(color="#FFDDCC", lake_color='#DDEEFF')
m.drawmapboundary(fill_color="#DDEEFF")
m.drawcoastlines()
ax[i].set_title("resolution='{0}'".format(res));

Notice that the low-resolution coastlines are not suitable for this level of zoom,
while high-resolution works just fine. The low level would work just fine for a
global view, however, and would be much faster than loading the high-resolution
border data for the entire globe! It might require some experimentation to find the
correct resolution parameter for a given view: the best route is to start with a fast,
low-resolution plot and increase the resolution as needed.

Plotting Data on Maps


Perhaps the most useful piece of the Basemap toolkit is the ability to over-plot a
variety of data onto a map background. For simple plotting and text,
any plt function works on the map; you can use the Basemap instance to project
latitude and longitude coordinates to (x, y) coordinates for plotting with plt, as we
saw earlier in the Seattle example.

In addition to this, there are many map-specific functions available as methods of


the Basemap instance. These work very similarly to their standard Matplotlib
counterparts, but have an additional Boolean argument latlon, which if set
to True allows you to pass raw latitudes and longitudes to the method, rather than
projected (x, y) coordinates.

Some of these map-specific methods are:

 contour()/contourf() : Draw contour lines or filled contours


 imshow(): Draw an image
 pcolor()/pcolormesh() : Draw a pseudocolor plot for irregular/regular
meshes
 plot(): Draw lines and/or markers.
 scatter(): Draw points with markers.
 quiver(): Draw vectors.
 barbs(): Draw wind barbs.
 drawgreatcircle(): Draw a great circle.

Seaborn: statistical data visualization

Seaborn helps to visualize the statistical relationships, To understand how


variables in a dataset are related to one another and how that relationship is
dependent on other variables, we perform statistical analysis. This Statistical
analysis helps to visualize the trends and identify various patterns in the dataset.
These are the plot will help to visualize:
 Line Plot
 Scatter Plot
 Box plot
 Point plot
 Count plot
 Violin plot
 Swarm plot
 Bar plot
 KDE Plot

Line plot:

Lineplot Is the most popular plot to draw a relationship between x and y with the
possibility of several semantic groupings.
Syntax : sns.lineplot(x=None, y=None)
Parameters:
x, y: Input data variables; must be numeric. Can pass data directly or reference
columns in data.
Let’s visualize the data with a line plot and pandas:
Example 1:
Python3

# import module

import seaborn as sns

import pandas

# loading csv

data = pandas.read_csv("nba.csv")

# plotting lineplot

sns.lineplot( data['Age'], data['Weight'])


Output:

Example 2: Use the hue parameter for plotting the graph.


Python3

# import module

import seaborn as sns

import pandas

# read the csv data

data = pandas.read_csv("nba.csv")

# plot

sns.lineplot(data['Age'],data['Weight'], hue
=data["Position"])

Output:

Scatter Plot:

Scatterplot Can be used with several semantic groupings which can help to
understand well in a graph against continuous/categorical data. It can draw a two-
dimensional graph.
Syntax: seaborn.scatterplot(x=None, y=None)
Parameters:
x, y: Input data variables that should be numeric.
Returns: This method returns the Axes object with the plot drawn onto it.
Let’s visualize the data with a scatter plot and pandas:
Example 1:
Python3

# import module

import seaborn
import pandas

# load csv

data = pandas.read_csv("nba.csv")

# plotting

seaborn.scatterplot(data['Age'],data['Weig
ht'])

Output:

Example 2: Use the hue parameter for plotting the graph.


Python3
import seaborn

import pandas

data = pandas.read_csv("nba.csv")

seaborn.scatterplot( data['Age'],
data['Weight'], hue =data["Position"])

Output:

Box plot:

A box plot (or box-and-whisker plot) s is the visual representation of the


depicting groups of numerical data through their quartiles against
continuous/categorical data.
A box plot consists of 5 things.
 Minimum
 First Quartile or 25%
 Median (Second Quartile) or 50%
 Third Quartile or 75%
 Maximum
Syntax:
seaborn.boxplot(x=None, y=None, hue=None, data=None)
Parameters:
 x, y, hue: Inputs for plotting long-form data.
 data: Dataset for plotting. If x and y are absent, this is interpreted as wide-
form.
Returns: It returns the Axes object with the plot drawn onto it.
Draw the box plot with Pandas:
Example 1:
Python3

# import module

import seaborn as sns

import pandas

# read csv and plotting

data = pandas.read_csv( "nba.csv" )

sns.boxplot( data['Age'] )

Output:
Example 2:
Python3

# import module

import seaborn as sns

import pandas

# read csv and plotting

data = pandas.read_csv( "nba.csv" )

sns.boxplot( data['Age'], data['Weight'])

Output:
Violin Plot:

A violin plot is similar to a boxplot. It shows several quantitative data across one
or more categorical variables such that those distributions can be compared.

Syntax: seaborn.violinplot(x=None, y=None, hue=None, data=None)


Parameters:
 x, y, hue: Inputs for plotting long-form data.
 data: Dataset for plotting.
Draw the violin plot with Pandas:
Example 1:
Python3

# import module

import seaborn as sns

import pandas
# read csv and plot

data = pandas.read_csv("nba.csv")

sns.violinplot(data['Age'])

Output:

Example 2:
Python3

# import module

import seaborn

seaborn.set(style = 'whitegrid')
# read csv and plot

data = pandas.read_csv("nba.csv")

seaborn.violinplot(x ="Age", y
="Weight",data = data)

Output:

You might also like