Skip to content
Home » matplotlib Part 5 – Adding Axes to the Figure

matplotlib Part 5 – Adding Axes to the Figure

Spread the love

In this part of the Matplotlib series we’ll see how to add Axes instances to a figure.

To add an Axes instance, we use the add_axes method. When adding an Axes, we need to assign it to a region within the figure. To do that, we have to specify the coordinates of it’s bottom left corner and its size. The coordinates, width and height are proportional rather than absolute, so they are in the range between 0 and 1.

In [1]:
%matplotlib inline
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np

# Let's create a figure with size 8 x 6 inches and set its background color.
fig = plt.figure(figsize=(8, 6), facecolor='yellow')

# Let's add three Axes instances:
ax1 = fig.add_axes((.1, .1, .5, .4), facecolor='cyan')
ax2 = fig.add_axes((.3, .6, .3, .3), facecolor='cyan')
ax3 = fig.add_axes((.7, .1, .2, .8), facecolor='cyan')

# Let's define the x and y values. We'll use the sine function from the previous lectures.
x = np.linspace(-10, 10, 100)
y = 10 * np.sin(x)

# Let's plot the sine function.
ax1.plot(x, y, color='green')
ax2.plot(x, y, color='green')
ax3.plot(x, y, color='green')
Out[1]:
[<matplotlib.lines.Line2D at 0x1fcd14d3b88>]

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.

Here’s the video version of this article:


Spread the love
Tags:

Leave a Reply