Matplotlib
Matplotlib
What is Matplotlib?
Matplotlib is a low level graph plotting library in python that serves as a
In [ ]:
The plot() function is used to draw points (markers) in a diagram.
In [2]:
import matplotlib.pyplot as plt
import numpy as np
plt.plot(xpoints, ypoints)
plt.show()
In [ ]:
Plotting Without Line
To plot only the markers, you can use shortcut string notation parameter 'o
In [3]:
#Draw two points in the diagram, one at position (1, 3) and one in position
In [4]:
#Draw a line in a diagram from position (1, 3) to (2, 8) then to (6, 1) and
plt.plot(xpoints, ypoints)
plt.show()
In [ ]:
Default X-Points
If we do not specify the points in the x-axis,
they will get the default values 0, 1, 2, 3, (etc. depending on the length
So, if we take the same example as above, and leave out the x-points,
the diagram will look like this:
In [5]:
#Plotting without x-points:
plt.plot(ypoints)
plt.show()
In [ ]:
Markers
You can use the keyword argument marker to emphasize each point with a spec
In [10]:
#Mark each point with a circle:
In [ ]:
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
'<' Triangle Left
'>' Triangle Right
'1' Tri Down
'2' Tri Up
'3' Tri Left
'4' Tri Right
'|' Vline
'_' Hline
In [ ]:
Format Strings:
fmt
You can use also use the shortcut string notation parameter to specify the
This parameter is also called fmt, and is written with this syntax:
marker|line|color
In [9]:
#Mark each point with a circle:
plt.plot(ypoints, 'o:r')
plt.show()
In [ ]:
Line Reference
In [ ]:
Color Reference
'r' Red
'g' Green
'b' Blue
'c' Cyan
'm' Magenta
'y' Yellow
'k' Black
'w' White
In [ ]:
Marker Size
You can use the keyword argument markersize or the shorter version,
ms to set the size of the markers:
In [ ]:
Marker Color
You can use the keyword argument markeredgecolor
or the shorter mec to set the color of the edge of the markers:
In [1]:
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
Matplotlib Line
Linestyle
You can use the keyword argument linestyle, or shorter ls, to change the st
In [11]:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
In [ ]:
linestyle can be written as ls.
In [7]:
import matplotlib.pyplot as plt
import numpy as np
#plt.plot(ypoints, ls = ':')
plt.plot(ypoints, ls = '--')
plt.show()
In [ ]:
Line Styles
You can choose any of these styles:
In [ ]:
Line Color
You can use the keyword argument color or the shorter c to set the color of
In [15]:
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
Line Width
You can use the keyword argument linewidth or the shorter lw to change the
In [16]:
import matplotlib.pyplot as plt
import numpy as np
In [17]:
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()
In [ ]:
You can also plot many lines by adding the points for the x- and y-axis for
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
In [18]:
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])
In [ ]:
Matplotlib Labels and Title
Create Labels for a Plot
With Pyplot, you can use the xlabel() and ylabel() functions to set a label
In [20]:
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()
In [ ]:
Create a Title for a Plot
With Pyplot, you can use the title() function to set a title for the plot.
In [21]:
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.show()
In [ ]:
Set Font Properties for Title and Labels
You can use the fontdict parameter in xlabel(), ylabel(), and title() to se
In [22]:
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.plot(x, y)
plt.show()
In [ ]:
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'
In [23]:
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.show()
In [ ]:
Add Grid Lines to a Plot
With Pyplot, you can use the grid() function to add grid lines to the plot
In [8]:
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.grid()
plt.show()
In [ ]:
Specify Which Grid Lines to Display
You can use the axis parameter in the grid() function to specify which grid
Legal values are: 'x', 'y', and 'both'. Default value is 'both'.
In [9]:
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.grid(axis = 'x')
plt.grid(axis = 'x')
#plt.grid(axis = 'both')
plt.show()
In [ ]:
Set Line Properties for the Grid
You can also set the line properties of the grid,
like this: grid(color = 'color', linestyle = 'linestyle', linewidth = numbe
In [10]:
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.show()
In [ ]:
Display Multiple Plots
With the subplot() function you can draw multiple plots in one figure:
The subplot() Function
The subplot() function takes three arguments that describes the layout of t
The layout is organized in rows and columns, which are represented by the f
In [28]:
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()
In [ ]:
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
In [29]:
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()
In [11]:
#Draw 6 plots:
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()
In [12]:
#2 plots, with titles:
#Add a title for the entire figure:
#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()
In [ ]:
Creating Scatter Plots
With Pyplot, you can use the scatter() function to draw a scatter plot.
In [30]:
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()
In [31]:
#Draw two plots on the same figure:
plt.show()
In [13]:
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 = 'green')
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 = 'red')
plt.show()
In [33]:
#Set your own color of the markers:
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","p
plt.scatter(x, y, color=colors)
plt.show()
In [35]:
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()
In [ ]:
Alpha
You can adjust the transparency of the dots with the alpha argument.
In [36]:
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.show()
In [ ]:
Creating Bars
With Pyplot, you can use the bar() function to draw bar graphs:
In [14]:
#Draw 4 bars:
plt.bar(x,y)
plt.show()
In [38]:
#The bar() function takes arguments that describes the layout of the bars.
#The categories and their values represented by the first and second argume
x = ["APPLES", "BANANAS"]
y = [400, 350]
plt.bar(x, y)
Example
Draw 4 horizontal bars:
In [15]:
import matplotlib.pyplot as plt
import numpy as np
plt.barh(x, y)
plt.show()
In [ ]:
Bar Color
The bar() and barh() takes the keyword argument color to set the color of t
In [39]:
import matplotlib.pyplot as plt
import numpy as np
Example
Draw 4 very thin bars:
In [40]:
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
Bar Height
The barh() takes the keyword argument height to set the height of the bars
In [41]:
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
Histogram
A histogram is a graph showing frequency distributions.
Example: Say you ask for the height of 250 people, you might end up with a
In [ ]:
Create Histogram
In Matplotlib, we use the hist() function to create histograms.
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
Learn more about Normal Data Distribution in our Machine Learning Tutorial
In [16]:
import numpy as np
print(x)
In [42]:
import matplotlib.pyplot as plt
import numpy as np
plt.hist(x)
plt.show()
In [ ]:
Creating Pie Charts
With Pyplot, you can use the pie() function to draw pie charts:
In [43]:
import matplotlib.pyplot as plt
import numpy as np
plt.pie(y)
plt.show()
In [ ]:
Labels
Add labels to the pie chart with the label parameter.
The label parameter must be an array with one label for each wedge:
In [44]:
import matplotlib.pyplot as plt
import numpy as np
In [ ]:
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.
In [17]:
#Start the first wedge at 90 degrees:
In [ ]:
Explode
Maybe you want one of the wedges to stand out? The explode parameter allows
The explode parameter, if specified, and not None, must be an array with on
Each value represents how far from the center each wedge is displayed:
In [19]: #Pull the "Apples" wedge 0.2 from the center of the pie:
In [ ]:
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 eac
In [20]:
import matplotlib.pyplot as plt
import numpy as np
In [ ]: