Matplotlib is an open source tool kit for representing and visualizing data. It was created by John Hunter, who was inspired by the MATLAB.
Matplotlib can also work outside of Jupyter, but Jupyter notebooks have special support of matplotlib that is inabled by using an “iPython magic,” or helper function,
This command configures matplotlib to render, interactively, into the browser.
This is known as “configuring the backend,” where the term backend refers to an abstraction layer that knows how to render matplotlib commands to its environment. These environments could be operating systems, a browser, or “hard copy backends” that support rendering to graphics files like SVGs or PNGs. The backend knows about low level graphics routines that can render to a file or to a screen.
import matplotlib as mpl mpl.get_backend()
So, ‘nbAgg’ is the name of the backend, which The backend itself is one of three layers.
The inline backend, shown next, has a different backend. It is a different way of rendering to the browser.
%matplotlib inline mpl.get_backend()
The other two layers are the artist and scripting layers.
The artist layer is an abstraction that deals with drawing and layout. The root (or “bottom”) part of matplotlib visuals is a set of container items that incldue a figure object with one or more subplots, each of which has a series of one or more axes.
axes are the object that will be interacted with most commonly, to perform such commands as changing the range of an axis. axes are made up of two axis, one each for the horizontal, x, and vertical, y, dimensions.
The artist layer contains many base drawing items like “rectangles,” “elipses,” and “lines,” as well as collections of items, like a “path.” There’s many child objects of the artitsts object, including “patches.patch,” which is any two-dimensional object that has a face color and an edge color.
The scripting layer used in these notes is called pyplot.
import matplotlib.pyplot as plt
The scripting layer would not be used at all if an application were being used to use matplotlib directly, but it is a tool that is very important for data scientists. It helps simplify and speed up our interaction with the environment in order to build plots quickly. It accomplishes this by doing a bunch of magic for us.
So, in summary:
- Backend layer: deals with actual drawing,
- Artists layer: several of these sit “on top of” the backend, and describe how data is arranged.
- Scripting layer: creates the artists and choreographs them together into our visual.
Procedural versus Declarative Visualization Libraries
The pyplot scripting layer is a procedural method for building a visualization. This means we tell the software the explicit actions to take in order to render the data.
An example of declarative method for visualizing data is HTML, where rather than issuing commands to a rendering agent, HTML documents actually consist of models of relationships in a document. This is called the Document Object Model.
Plotting using the Scripting Layer
%matplotlib notebook plt.plot(3, 2, '.')
[<matplotlib.lines.Line2D at 0x118e55fd0>]
Using the “inline” backend displays images directly in the browser.
%matplotlib inline plt.plot(3, 2, '.')
[<matplotlib.lines.Line2D at 0x11721ab70>]
Behind the scenes, the scipting layer is using the function
gcf to get the current figure, and
gca to get the current axes. Pyplot keeps track of these objects automatically, taking care of all interactions with artists for the user.
plot method on the scripting layer
plt actually just executes
Plotting without using the Scripting Layer
It is also possible to plot without using the scripting layer at all. The following code interacts with artists directly, and saves an image (
test.png) to the root directory of the notebook.
from matplotlib.backends.backend_agg import FigureCanvasAgg from matplotlib.figure import Figure # create a new figure fig = Figure() # associate the new fig with the backend canvas = FigureCanvasAgg(fig) ax = fig.add_subplot(111) ax.plot(3, 2, '.') # save the figure to test.png canvas.print_png('test.png')
This image can be viewed using an HTML magic.
%%html <img src='test.png' />
Using the Scripting Layer and Accessing Underlying Artists
%matplotlib notebook plt.figure() plt.plot(1.5, 1.5, 'o') plt.plot(2, 2, 'o') plt.plot(2.5, 2.5, 'o')
[<matplotlib.lines.Line2D at 0x118ebd4e0>]
The various artists are shown below.
ax = plt.gca() ax.get_children()
[<matplotlib.lines.Line2D at 0x118ebd198>, <matplotlib.lines.Line2D at 0x118e5e8d0>, <matplotlib.lines.Line2D at 0x118ebd4e0>, <matplotlib.spines.Spine at 0x118e98400>, <matplotlib.spines.Spine at 0x118e984e0>, <matplotlib.spines.Spine at 0x118e985f8>, <matplotlib.spines.Spine at 0x118e98710>, <matplotlib.axis.XAxis at 0x118e98390>, <matplotlib.axis.YAxis at 0x118e98b00>, Text(0.5, 1.0, ''), Text(0.0, 1.0, ''), Text(1.0, 1.0, ''), <matplotlib.patches.Rectangle at 0x118ea5cf8>]