Plots with matplotlib¶
Basic plotting¶
We can create plots with Python using matplotlib.pyplot
module.
First we import the module:
import matplotlib.pyplot as plt
Each function in matplotlib.pyplot
can be now accessed by typing
plt.function_name
.
Here are the main functions we will use. Click on the function description to see its full documentation:
function |
usage |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
The syntax of the plot
function is plot(xl, yl, options)
where:
xl
is the list of x-coordinates of points we want to plotyl
is the list of y-coordinatesother options may specify how we want the plot to look like.
The show
function must be executed to display a plot.
xl = range(-10, 11) #x-coordinates
yl = [x**2 for x in xl] #y-coordinates
plt.plot(xl, yl) #create plot
plt.ylim(-10, 110) #set the range of y-values displayed
plt.title('My first plot') #set the plot title
plt.xlabel('This is the x-axis') #set the label of the x-axis
plt.ylabel('This is the y-axis') #set the label of the y-axis
plt.show() #show plot - not necessary in the inline mode
Notice that on the plot above the x-axis and the y-axis have different
scales: the distance between 0 and 5 on the x-axis is bigger than the
distance between 0 and 20 on the y-axis. In order to set the same scale
for both axes we can use the axis
function:
plt.plot(xl, yl) #create plot
plt.axis('equal') #equalize axes
plt.ylim(-10, 100) #set the range of y-values displayed
plt.show()
The axis
function has other options that can be used to control
properties of coordinate axes of a plot. See matplotlib
documentation
for details.
More plotting options¶
By default the plot
function joins the specified points by straight
lines. If, instead, we want to plot individual points as red circles we
can add the 'ro'
option ('r'
specifies the color, and 'o'
the shape of markers):
plt.plot(xl, yl, 'ro')
plt.xlim()
plt.ylim(-10, 110)
plt.show()
Here are same other options for shapes and colors of markers:
shape |
meaning |
color |
meaning |
---|---|---|---|
|
solid line |
|
blue |
|
dashed line |
|
green |
|
point marker |
|
red |
|
circle marker |
|
cyan |
|
square marker |
|
white |
|
plus marker |
|
magenta |
|
x marker |
|
yellow |
|
diamond marker |
|
black |
Here are some examples:
x_list = range(20)
y1_list = 20*[1]
y2_list = 20*[2]
y3_list = 20*[3]
y4_list = 20*[4]
plt.xlim(-1,20)
plt.ylim(0,5)
plt.plot(x_list, y1_list, 'b.',)
plt.plot(x_list, y2_list, 'gx')
plt.plot(x_list, y3_list, 'm.')
plt.plot(x_list, y4_list, 'rs')
plt.title('Shapes and colors')
plt.show()
The parameter ms
of the plot
function can be used to set the
size of markers:
plt.xlim(-1,20)
plt.ylim(0,5)
plt.plot(x_list, y1_list, 'rD', ms=2)
plt.plot(x_list, y2_list, 'rD', ms=4)
plt.plot(x_list, y3_list, 'rD', ms=8)
plt.plot(x_list, y4_list, 'rD', ms=12)
plt.title('Sizes of markers')
plt.show()
The parameter alpha
controls transparency of plots. The value of
alpha
can vary between 0 (completely transparent) to 1 (opaque):
plt.xlim(-1,20)
plt.ylim(0,5)
plt.plot(x_list, y1_list, 'rD', ms=20, alpha= 0.25)
plt.plot(x_list, y2_list, 'rD', ms=20, alpha= 0.5)
plt.plot(x_list, y3_list, 'rD', ms=20, alpha= 0.75)
plt.plot(x_list, y4_list, 'rD', ms=20, alpha= 1.0)
plt.title('Transparency')
plt.show()
Plot legend¶
If more than one graph is plotted on a single figure a legend can be used to label graphs:
x_list = [0.01*x for x in range(-200, 201)]
y2_list = [x**2 for x in x_list]
y4_list = [-x**2 for x in x_list]
y6_list = [-x**3 + x for x in x_list]
plt.plot(x_list, y2_list, 'b-', label='$y=x^2$') # label specifies the plot description in the legend
plt.plot(x_list, y4_list, 'r-', label='$y=-x^2$') # LaTeX can be used to typeset formulas in matplotlib labels and titles
plt.plot(x_list, y6_list, 'g-', label='$y=x^3+x$')
plt.ylim(-1, 1)
plt.xlim(-3, 3)
plt.legend(fontsize=13) # creates the legend; fontsize specifies the size of fonts in the legend
plt.show()
Exercise 1. Plot the function \(y = \sin(6x)\) for \(0\leq x \leq 6\). The Python function
sin
which computes values of sine is a part of the math
module:
from math import sin, pi
sin(pi/2)
1.0
Further reading¶
Matplotlib is a very large module with a lot of uses. Here are a couple links that can be helpful for exploring it further: