An introduction to Matplotlib
Ananda Dasgupta
Indian Institute of Science Education and Research Kolkata
3-4 May, 2019
Ananda Dasgupta Matplotlib 1/6
Starting out with Numpy
The basic mantra
import matplotlib as mpl
import [Link] as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
Ananda Dasgupta Matplotlib 2/6
Starting out with Numpy
The basic mantra
import matplotlib as mpl
import [Link] as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
We will also need
import numpy as np
Ananda Dasgupta Matplotlib 2/6
Starting out with Numpy
The basic mantra
import matplotlib as mpl
import [Link] as plt
from mpl_toolkits.mplot3d.axes3d import Axes3D
We will also need
import numpy as np
Once this is done, you are ready to create the graphs of your
dreams!!
Ananda Dasgupta Matplotlib 2/6
My rst graph
Let's plot sin(5x ), e −x /2 and their product from x = 0 to
x = 10 on the same graph.
Ananda Dasgupta Matplotlib 3/6
My rst graph
Let's plot sin(5x ), e −x /2 and their product from x = 0 to
x = 10 on the same graph.
First, set up an array of x values:
>>> x = [Link](0,10,1000)
(1000 equally spaced points between 0 and 2, both inclusive)
Ananda Dasgupta Matplotlib 3/6
My rst graph
Let's plot sin(5x ), e −x /2 and their product from x = 0 to
x = 10 on the same graph.
First, set up an array of x values:
>>> x = [Link](0,10,1000)
(1000 equally spaced points between 0 and 2, both inclusive)
Create array's for the functions to be plotted:
>>> y1 = [Link](5*x)
>>> y2 = [Link](-x/2)
>>> y3 = y1*y2
Ananda Dasgupta Matplotlib 3/6
My rst graph
Let's plot sin(5x ), e −x /2 and their product from x = 0 to
x = 10 on the same graph.
First, set up an array of x values:
>>> x = [Link](0,10,1000)
(1000 equally spaced points between 0 and 2, both inclusive)
Create array's for the functions to be plotted:
>>> y1 = [Link](5*x)
>>> y2 = [Link](-x/2)
>>> y3 = y1*y2
Now, set up the graphs:
>>> [Link](x,y1,color='blue')
>>> [Link](x,y2,color='green')
>>> [Link](x,y3,color='red')
Ananda Dasgupta Matplotlib 3/6
My rst graph
Let's plot sin(5x ), e −x /2 and their product from x = 0 to
x = 10 on the same graph.
First, set up an array of x values:
>>> x = [Link](0,10,1000)
(1000 equally spaced points between 0 and 2, both inclusive)
Create array's for the functions to be plotted:
>>> y1 = [Link](5*x)
>>> y2 = [Link](-x/2)
>>> y3 = y1*y2
Now, set up the graphs:
>>> [Link](x,y1,color='blue')
>>> [Link](x,y2,color='green')
>>> [Link](x,y3,color='red')
This sets up the graphs - but to actually display them - you
need:
>>> [Link]()
Ananda Dasgupta Matplotlib 3/6
My rst graph
Quick and easy!
Ananda Dasgupta Matplotlib 4/6
Taking control
Using the default Figure and Axes instances, as we have
done, make details dicult to control.
Ananda Dasgupta Matplotlib 5/6
Taking control
Using the default Figure and Axes instances, as we have
done, make details dicult to control.
We can get more control by creating instances explicitly.
Ananda Dasgupta Matplotlib 5/6
Taking control
Using the default Figure and Axes instances, as we have
done, make details dicult to control.
We can get more control by creating instances explicitly.
A simple way:
g,ax = [Link]()
Ananda Dasgupta Matplotlib 5/6
Taking control
Using the default Figure and Axes instances, as we have
done, make details dicult to control.
We can get more control by creating instances explicitly.
A simple way:
g,ax = [Link]()
Now, we can use the Axes instance ax to congure the plots:
>>> [Link](x,y1,color='blue',legend='sin(5x)')
>>> [Link](x,y2,color='green',legend='exp(-x/2)')
>>> [Link](x,y3,color='red',legend='sin(5x)exp(-x/2)')
Ananda Dasgupta Matplotlib 5/6
Taking control
Using the default Figure and Axes instances, as we have
done, make details dicult to control.
We can get more control by creating instances explicitly.
A simple way:
g,ax = [Link]()
Now, we can use the Axes instance ax to congure the plots:
>>> [Link](x,y1,color='blue',legend='sin(5x)')
>>> [Link](x,y2,color='green',legend='exp(-x/2)')
>>> [Link](x,y3,color='red',legend='sin(5x)exp(-x/2)')
Finer touches : >>> ax.set_xlabel('x')
>>> ax.set_ylabel('y')
>>> [Link]()
Ananda Dasgupta Matplotlib 5/6
Taking control
Ananda Dasgupta Matplotlib 6/6