2.5. Introduction To Matplotlib 1
2.5. Introduction To Matplotlib 1
PYTHON PROGRAMMING
24CAH-606
2
Matplotlib
• Matplotlib Pyplot
3
Matplotlib
• Plotting Without Line
4
Matplotlib
5
Matplotlib
• You can use the keyword argument marker to emphasize each point
with a specified marker.
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, marker = 'o')
plt.show()
6
Matplotlib
• 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 of the y-points).
7
Matplotlib
• Line Properties: There are the following line properties:
• linestyle
• Color
• linewidth
• marker
• ms(marker size)
• mfc(marker color)
• title
• xlabel
• ylabel
• grid 8
Matplotlib
Marker Size
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()
10
Matplotlib
Marker Color
You can use the keyword argument markeredgecolor or the shorter mec to set
the color of the edge of the markers:
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()
11
Matplotlib
You can use the keyword argument markerfacecolor or the shorter mfc to set
the color inside the edge of the markers.
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()
12
Matplotlib
• Linestyle
13
Matplotlib
• Linestyle
14
Line Color
You can use the keyword argument color or the shorter c to set the
color of the line:
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([3, 8, 1, 10])
plt.plot(ypoints, color = 'r')
plt.show()
15
Line Width
16
Multiple Lines
You can plot as many lines as you like by simply adding more plt.plot()
functions:
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()
17
Matplotlib Labels and Title
18
Set Font Properties for Title and Labels
19
Set Font Properties for 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()
20
Add Grid Lines to a Plot
import numpy as np
import matplotlib.pyplot as plt
# X-axis values
x = [1, 2, 3, 4, 5]
# Y-axis values
y = [1, 4, 9, 16, 25]
# Function to plot
plt.plot(x, y)
# Function add a legend
plt.legend(['single element'])
# function to show the plot
plt.show()
21
Add Grid Lines to a Plot
You can use the axis parameter
in the grid() function to specify
which grid lines to display.
Example
plt.grid(axis = 'x')
22
Matplotlib
• Display Multiple Plots: 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.
23
Matplotlib
24
Matplotlib
• Python Matplotlib : Types of Plots
25
Matplotlib
• Types of Plots:
• Bar Graph: A bar graph uses bars to compare data among different categories. It
is well suited when you want to measure the changes over a period of time. It can
be represented horizontally or vertically.
• Histograms: Histograms are used to show a distribution whereas a bar chart is
used to compare different entities. Histograms are useful where arrays in very
large size.
• Scatter Plot : Usually scatter plots is used to compare variables, for example,
how much one variable is affected by another variable to build a relation out of it.
The data is displayed as a collection of points, each having the value of one
variable which determines the position on the horizontal axis and the value of
other variable determines the position on the vertical axis.
26
Matplotlib
• Area Plot : Area plots are pretty much similar to the line plot. They are also
known as stack plots. These plots can be used to track changes over time for two
or more related groups that make up one whole category.
• Pie Chart: A pie chart refers to a circular graph which is broken down into
segments i.e. slices of pie. It is basically used to show the percentage or
proportional data where each slice of pie represents a category.
27
Matplotlib
• Matplotlib Scatter
28
Matplotlib Scatter
Color Each Dot
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","b
eige","brown","gray","cyan","magenta"])
plt.scatter(x, y, c=colors)
plt.show()
29
Creating Bars
With Pyplot, you can use the bar() function to draw bar graphs:
import matplotlib.pyplot as plt
import numpy as np
plt.bar(x,y)
plt.show()
30
Horizontal Bars
If you want the bars to be displayed horizontally instead of vertically, use the barh()
function:
import matplotlib.pyplot as plt
import numpy as np
plt.barh(x, y)
plt.show()
31
Matplotlib
• Matplotlib Bars – default width is 0.8
• import matplotlib.pyplot as plt
32
Matplotlib Histograms
33
Matplotlib Pie Charts
plt.pie(y)
plt.show()
34
Matplotlib Pie Charts
Labels
Add labels to the pie chart with the labels
parameter.
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()
35
Matplotlib Pie Charts
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:
36
Matplotlib Pie Charts
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()
37
Matplotlib Pie Charts
Explode
Maybe you want one of the wedges to stand out? The explode parameter allows you to do
that.
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()
38
Matplotlib Pie Charts
Colors
You can set the color of each wedge with the colors parameter.
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()
39
Matplotlib Pie Charts
Legend
To add a list of explanation for each wedge, use the legend() function:
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()
40
Matplotlib Pie Charts
41
Matplotlib Area plot
An area chart is really similar to a line chart, except that the area
between the x axis and the line is filled in with color or shading. It
represents the evolution of a numeric variable.
There are 2 main ways to build an area chart with Matplotlib. In both
case it requires 2 numeric vectors of values as input.
• The fill_between() function
• The stackplot() function that is more useful for stacked area charts
42
Matplotlib Area plot
import numpy as np
import matplotlib.pyplot as plt
# Create data
x=range(1,6)
y=[1,4,6,8,4]
# Area plot
plt.fill_between(x, y)
plt.show()
43
Matplotlib Area plot
44
THANK YOU
45