The matplotlib Library
Haim Michael
October 14th
, 2020
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
lifemichael
© 2008 Haim Michael 20150729
What is Matplotlib?
 Matplotlib is a library that allows us to create static,
animated, and interactive visualizations in Python.
© 2008 Haim Michael 20150729
Installing Matplotlib
 The simplest way to install matplotlib would be to install it
using the pip utility.
pip install matplotlib
© 2008 Haim Michael 20150729
The plot Function
 There are various ways to use the matplotlib library for
drawing diagrams. The simplest would be using the plot
function, that was defined inside the matplotlib.pyplot
module, passing over the x and the y coordinates.
© 2008 Haim Michael 20150729
The show Function
 Once completed, calling show will draw the diagram on the
screen.
import matplotlib.pyplot as plt
import numpy as np
plt.plot([1,2,3,4], [4,3,2,1], label='linear')
plt.legend()
plt.show()
© 2008 Haim Michael 20150729
The show Function
© 2008 Haim Michael 20150729
The Figure Class
 When instantiating matplotlib.figure.Figure class
we get an object that represents a window (or a page) on
which everything will be plotted.
© 2008 Haim Michael 20150729
The add_subplot Method
 We can invoke add_sublot on a Figure object in order to
add axes for plotting a diagram.
© 2008 Haim Michael 20150729
The add_subplot Method
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.add_subplot()
ax.scatter([1,2,3,4,5],[4,3,7,8,20])
plt.show()
© 2008 Haim Michael 20150729
The subplots Function
 We can invoke the sublots function that was defined in the
matplotlib.pyplot module and get a tuple that holds a
reference for a Figure object and a reference for an Axes
object.
© 2008 Haim Michael 20150729
The subplots Function
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2, 20)
fig, ax = plt.subplots()
ax.plot(x, x, label='x=x')
ax.plot(x, x**2, label='x=x**2')
ax.plot(x, x**3, label='x=x**3')
ax.set_xlabel('x label')
ax.set_ylabel('y label')
ax.set_title("Simple Plot")
ax.legend()
plt.show()
© 2008 Haim Michael 20150729
The Interactive Mode
 We can turn on the interactive mode by calling the ion()
function.
 Doing so when using a Python console we will be able to
continue the execution of more statements in the Python
Console and while doing so, get to see the immediate
outcome.
© 2008 Haim Michael 20150729
The Interactive Mode
© 2008 Haim Michael 20150729
The plot() Function
 The plot() method that was defined inside the
matplotlib.pyplot module can be invoked in various
ways.
 The plot() method that was defined inside the
matplotlib.pyplot module can be invoked in various
ways.
© 2008 Haim Michael 20150729
The plot() Function
import matplotlib.pyplot as plt
import numpy as np
t = np.arange(0, 4, 0.1)
plt.plot(t, t, 'g--', t, t**2, 'bs', t, t**3, 'r^')
plt.show()
© 2008 Haim Michael 20150729
Q & A
 Thanks!
haim.michael@lifemichael.com
054-6655837
http://blog.lifemichael.com

The matplotlib Library

  • 1.
    The matplotlib Library HaimMichael October 14th , 2020 All logos, trade marks and brand names used in this presentation belong to the respective owners. lifemichael
  • 2.
    © 2008 HaimMichael 20150729 What is Matplotlib?  Matplotlib is a library that allows us to create static, animated, and interactive visualizations in Python.
  • 3.
    © 2008 HaimMichael 20150729 Installing Matplotlib  The simplest way to install matplotlib would be to install it using the pip utility. pip install matplotlib
  • 4.
    © 2008 HaimMichael 20150729 The plot Function  There are various ways to use the matplotlib library for drawing diagrams. The simplest would be using the plot function, that was defined inside the matplotlib.pyplot module, passing over the x and the y coordinates.
  • 5.
    © 2008 HaimMichael 20150729 The show Function  Once completed, calling show will draw the diagram on the screen. import matplotlib.pyplot as plt import numpy as np plt.plot([1,2,3,4], [4,3,2,1], label='linear') plt.legend() plt.show()
  • 6.
    © 2008 HaimMichael 20150729 The show Function
  • 7.
    © 2008 HaimMichael 20150729 The Figure Class  When instantiating matplotlib.figure.Figure class we get an object that represents a window (or a page) on which everything will be plotted.
  • 8.
    © 2008 HaimMichael 20150729 The add_subplot Method  We can invoke add_sublot on a Figure object in order to add axes for plotting a diagram.
  • 9.
    © 2008 HaimMichael 20150729 The add_subplot Method import matplotlib.pyplot as plt import numpy as np fig = plt.figure() ax = fig.add_subplot() ax.scatter([1,2,3,4,5],[4,3,7,8,20]) plt.show()
  • 10.
    © 2008 HaimMichael 20150729 The subplots Function  We can invoke the sublots function that was defined in the matplotlib.pyplot module and get a tuple that holds a reference for a Figure object and a reference for an Axes object.
  • 11.
    © 2008 HaimMichael 20150729 The subplots Function import matplotlib.pyplot as plt import numpy as np x = np.linspace(0, 2, 20) fig, ax = plt.subplots() ax.plot(x, x, label='x=x') ax.plot(x, x**2, label='x=x**2') ax.plot(x, x**3, label='x=x**3') ax.set_xlabel('x label') ax.set_ylabel('y label') ax.set_title("Simple Plot") ax.legend() plt.show()
  • 12.
    © 2008 HaimMichael 20150729 The Interactive Mode  We can turn on the interactive mode by calling the ion() function.  Doing so when using a Python console we will be able to continue the execution of more statements in the Python Console and while doing so, get to see the immediate outcome.
  • 13.
    © 2008 HaimMichael 20150729 The Interactive Mode
  • 14.
    © 2008 HaimMichael 20150729 The plot() Function  The plot() method that was defined inside the matplotlib.pyplot module can be invoked in various ways.  The plot() method that was defined inside the matplotlib.pyplot module can be invoked in various ways.
  • 15.
    © 2008 HaimMichael 20150729 The plot() Function import matplotlib.pyplot as plt import numpy as np t = np.arange(0, 4, 0.1) plt.plot(t, t, 'g--', t, t**2, 'bs', t, t**3, 'r^') plt.show()
  • 16.
    © 2008 HaimMichael 20150729 Q & A  Thanks! haim.michael@lifemichael.com 054-6655837 http://blog.lifemichael.com