5/27/2023
Dr. Papia Sultana
Professor, Dept. of Statistics
University of Rajshahi
Graphical plot
Introduce to graphing in python with Matplotlib,
which is arguably the most popular graphing and data
visualization library for Python.
The easiest way to install matplotlib is to use pip. Type
following command in terminal:
pip install matplotlib
1
5/27/2023
Getting started (plotting a line)
# importing the required module
import matplotlib.pyplot as plt
# x axis values
x = [1,2,3]
# corresponding y axis values
y = [2,4,1]
# plotting the points
plt.plot(x, y)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('My first graph!')
# function to show the plot
plt.show()
2
5/27/2023
Output:
The code seems self-explanatory. Following steps were
followed:
Define the x-axis and corresponding y-axis values as
lists.
Plot them on canvas using .plot() function.
Give a name to x-axis and y-axis
using .xlabel() and .ylabel() functions.
Give a title to your plot using .title() function.
Finally, to view your plot, we use .show() function.
3
5/27/2023
Plotting two or more lines on
same plot
import matplotlib.pyplot as plt
# line 1 points
x1 = [1,2,3]
y1 = [2,4,1]
# plotting the line 1 points
plt.plot(x1, y1, label = "line 1")
# line 2 points
x2 = [1,2,3]
y2 = [4,1,3]
# plotting the line 2 points
plt.plot(x2, y2, label = "line 2")
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Two lines on same graph!')
# show a legend on the plot
plt.legend()
# function to show the plot
plt.show()
4
5/27/2023
Output
Customization of Plots
mport matplotlib.pyplot as plt
# x axis values
x = [1,2,3,4,5,6]
# corresponding y axis values
y = [2,4,1,5,2,6]
# plotting the points
plt.plot(x, y, color='green', linestyle='dashed', linewidth = 3,
marker='o', markerfacecolor='blue', markersize=12)
5
5/27/2023
# setting x and y axis range
plt.ylim(1,8)
plt.xlim(1,8)
# naming the x axis
plt.xlabel('x - axis')
# naming the y axis
plt.ylabel('y - axis')
# giving a title to my graph
plt.title('Some cool customizations!')
# function to show the plot
plt.show()
Output
6
5/27/2023
Bar Chart
import matplotlib.pyplot as plt
# x-coordinates of left sides of bars
left = [1, 2, 3, 4, 5]
# heights of bars
height = [10, 24, 36, 40, 5]
# labels for bars
tick_label = ['one', 'two', 'three', 'four', 'five']
# plotting a bar chart
plt.bar(left, height, tick_label = tick_label,
width = 0.8, color = ['red', 'green'])
# naming the x-axis
plt.xlabel('x - axis')
# naming the y-axis
plt.ylabel('y - axis')
# plot title
plt.title('My bar chart!')
# function to show the plot
plt.show()
7
5/27/2023
Output
Histogram
plt.hist() function is used to plot a histogram.
8
5/27/2023
Histogram
import matplotlib.pyplot as plt
# frequencies
ages = [2,5,70,40,30,45,50,45,43,40,44,
60,7,13,57,18,90,77,32,21,20,40]
# setting the ranges and no. of intervals
range = (0, 100)
bins = 10
# plotting a histogram
plt.hist(ages, bins, range, color = 'green',
histtype = 'bar', rwidth = 0.8)
# x-axis label
plt.xlabel('age')
# frequency label
plt.ylabel('No. of people')
# plot title
plt.title('My histogram')
# function to show the plot
plt.show()
9
5/27/2023
Output
Scatter plot
plt.scatter() function is used to plot a scatter plot.
10
5/27/2023
import matplotlib.pyplot as plt
# x-axis values
x = [1,2,3,4,5,6,7,8,9,10]
# y-axis values
y = [2,4,5,7,6,8,9,11,12,12]
# plotting points as a scatter plot
plt.scatter(x, y, label= "stars", color= "green",
marker= "*", s=30)
# x-axis label
plt.xlabel('x - axis')
# frequency label
plt.ylabel('y - axis')
# plot title
plt.title('My scatter plot!')
# showing legend
plt.legend()
# function to show the plot
plt.show()
11
5/27/2023
Here, we use plt.scatter() function to plot a scatter
plot.
As a line, we define x and corresponding y-axis values
here as well.
marker argument is used to set the character to use as
a marker. Its size can be defined using the s parameter.
output
12
5/27/2023
Pie-chart
import matplotlib.pyplot as plt
# defining labels
activities = ['eat', 'sleep', 'work', 'play']
# portion covered by each label
slices = [3, 7, 8, 6]
# color for each label
colors = ['r', 'y', 'g', 'b']
# plotting the pie chart
plt.pie(slices, labels = activities, colors=colors,
startangle=90, shadow = True, explode = (0, 0, 0.1, 0),
radius = 1.2, autopct = '%1.1f%%')
# plotting legend
plt.legend()
# showing the plot
plt.show()
Here, we plot a pie chart by using plt.pie() method.
First of all, we define the labels using a list
called activities.
Then, a portion of each label can be defined using another
list called slices.
Color for each label is defined using a list called colors.
shadow = True will show a shadow beneath each label in
pie chart.
startangle rotates the start of the pie chart by given
degrees counterclockwise from the x-axis.
explode is used to set the fraction of radius with which we
offset each wedge.
autopct is used to format the value of each label. Here, we
have set it to show the percentage value only upto 1 decimal
place.
13
5/27/2023
Output
Plotting curves of given equation
# importing the required modules
import matplotlib.pyplot as plt
import numpy as np
# setting the x - coordinates
x = np.arange(0, 2*(np.pi), 0.1)
# setting the corresponding y - coordinates
y = np.sin(x)
# plotting the points
plt.plot(x, y)
# function to show the plot
plt.show()
14
5/27/2023
Here, we use NumPy which is a general-purpose array-
processing package in python.
To set the x-axis values, we
use the np.arange() method in which the first two
arguments are for range and the third one for step-wise
increment. The result is a NumPy array.
To get corresponding y-axis values, we simply use the
predefined np.sin() method on the NumPy array.
Finally, we plot the points by passing x and y arrays to
the plt.plot() function.
output
15
5/27/2023
Subplots
Subplots are required when we want to show two or
more plots in same figure. We can do it in two ways
using two slightly different methods.
# importing required modules
import matplotlib.pyplot as plt
import numpy as np
# function to generate coordinates
def create_plot(ptype):
# setting the x-axis values
x = np.arange(-10, 10, 0.01)
# setting the y-axis values
if ptype == 'linear':
y=x
elif ptype == 'quadratic':
y = x**2
elif ptype == 'cubic':
y = x**3
elif ptype == 'quartic':
y = x**4
return(x, y)
16
5/27/2023
# setting a style to use
plt.style.use('fivethirtyeight')
# create a figure
fig = plt.figure()
# define subplots and their positions in figure
plt1 = fig.add_subplot(221)
plt2 = fig.add_subplot(222)
plt3 = fig.add_subplot(223)
plt4 = fig.add_subplot(224)
# plotting points on each subplot
x, y = create_plot('linear')
plt1.plot(x, y, color ='r')
plt1.set_title('$y_1 = x$')
x, y = create_plot('quadratic')
plt2.plot(x, y, color ='b')
plt2.set_title('$y_2 = x^2$')
x, y = create_plot('cubic')
plt3.plot(x, y, color ='g')
plt3.set_title('$y_3 = x^3$')
x, y = create_plot('quartic')
plt4.plot(x, y, color ='k')
plt4.set_title('$y_4 = x^4$')
17
5/27/2023
# adjusting space between subplots
fig.subplots_adjust(hspace=.5,wspace=0.5)
# function to show the plot
plt.show()
output
18