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 matplotlib.pyplot 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 matplotlib.pyplot 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 matplotlib.pyplot 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 = np.linspace(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 = np.linspace(0,10,1000)
(1000 equally spaced points between 0 and 2, both inclusive)
Create array's for the functions to be plotted:
>>> y1 = np.sin(5*x)
>>> y2 = np.exp(-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 = np.linspace(0,10,1000)
(1000 equally spaced points between 0 and 2, both inclusive)
Create array's for the functions to be plotted:
>>> y1 = np.sin(5*x)
>>> y2 = np.exp(-x/2)
>>> y3 = y1*y2
Now, set up the graphs:
>>> plt.plot(x,y1,color='blue')
>>> plt.plot(x,y2,color='green')
>>> plt.plot(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 = np.linspace(0,10,1000)
(1000 equally spaced points between 0 and 2, both inclusive)
Create array's for the functions to be plotted:
>>> y1 = np.sin(5*x)
>>> y2 = np.exp(-x/2)
>>> y3 = y1*y2
Now, set up the graphs:
>>> plt.plot(x,y1,color='blue')
>>> plt.plot(x,y2,color='green')
>>> plt.plot(x,y3,color='red')
This sets up the graphs - but to actually display them - you
need:
>>> plt.show()
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 = plt.subplots()
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 = plt.subplots()
Now, we can use the Axes instance ax to congure the plots:
>>> ax.plot(x,y1,color='blue',legend='sin(5x)')
>>> ax.plot(x,y2,color='green',legend='exp(-x/2)')
>>> ax.plot(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 = plt.subplots()
Now, we can use the Axes instance ax to congure the plots:
>>> ax.plot(x,y1,color='blue',legend='sin(5x)')
>>> ax.plot(x,y2,color='green',legend='exp(-x/2)')
>>> ax.plot(x,y3,color='red',legend='sin(5x)exp(-x/2)')
Finer touches : >>> ax.set_xlabel('x')
>>> ax.set_ylabel('y')
>>> ax.legend()
Ananda Dasgupta Matplotlib 5/6
Taking control
Ananda Dasgupta Matplotlib 6/6