Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Creating a Canvas | Matplotlib Introduction
course content

Contenido del Curso

Ultimate Visualization with Python

Creating a CanvasCreating a Canvas

First of all, matplotlib consists of three layers:

  • Backend layer (renders plot to screens or files);
  • Artist layer (describes how data is arranged, is made up of one object, Artist);
  • Scripting layer (connects the previous two layers and simplifies access to them).

We will mainly focus on the scripting layer with the pyplot module you have already seen and the artist layer. Artist layer contains the following:

  • Containers (e.g. Figure, Axes);
  • Primitives (e.g. Line, Rectangle, Circle, Text, etc).

Figure is the main Artist object and can be thought of as a canvas where all the plots will be located. Basically, it holds everything together.

On the other hand, Axes is an object made up of two axis objects, x-axis and y-axis.

Note

Figure = canvas, Axes = x-axis + y-axis.

Now let’s look at the creation of the Figure and its Axes:

Code Description
import matplotlib.pyplot as plt

First, we import the pyplot submodule as plt.

fig, ax = plt.subplots()

Then we use its subplots() function, which returns a figure and one Axes object by default.

ax.plot()

Next we apply the plot() function to this Axes object to create an empty plot.

plt.show()

Finally, using the show() function, we display the image of our canvas to the screen with one empty plot on it.

¿Todo estuvo claro?

Sección 1. Capítulo 3
course content

Contenido del Curso

Ultimate Visualization with Python

Creating a CanvasCreating a Canvas

First of all, matplotlib consists of three layers:

  • Backend layer (renders plot to screens or files);
  • Artist layer (describes how data is arranged, is made up of one object, Artist);
  • Scripting layer (connects the previous two layers and simplifies access to them).

We will mainly focus on the scripting layer with the pyplot module you have already seen and the artist layer. Artist layer contains the following:

  • Containers (e.g. Figure, Axes);
  • Primitives (e.g. Line, Rectangle, Circle, Text, etc).

Figure is the main Artist object and can be thought of as a canvas where all the plots will be located. Basically, it holds everything together.

On the other hand, Axes is an object made up of two axis objects, x-axis and y-axis.

Note

Figure = canvas, Axes = x-axis + y-axis.

Now let’s look at the creation of the Figure and its Axes:

Code Description
import matplotlib.pyplot as plt

First, we import the pyplot submodule as plt.

fig, ax = plt.subplots()

Then we use its subplots() function, which returns a figure and one Axes object by default.

ax.plot()

Next we apply the plot() function to this Axes object to create an empty plot.

plt.show()

Finally, using the show() function, we display the image of our canvas to the screen with one empty plot on it.

¿Todo estuvo claro?

Sección 1. Capítulo 3
some-alt