Figure
and the coordinate system Axes
¶Basically it is recommended to generate as many Axes as you need at the same time as creating the figure.
However, if you want to create a weird Axes or have an Axes in a weird arrangement, first create a figure of the required size with figure()
.
matplotlib.figure.Figure(*args, **kwargs) [Parameters] figsize=(6.4, 4.8) : (width, height) dimension in inches dpi=100 : dots per inch [Returns] Artist: the added artist
Then use figure.add_axes(rect=[left, bottom, width, height])
to place new Axes in any position you like.
matplotlib.figure.Figure.add_axes(self, *args, **kwargs) [Parameters] rect: [left, bottom, width, height] projection: projection types sharex, sharey: share x or y axis label: label for the returned axes [Returns] axes
In the example below, it is specified as [left, bottom, width, height] = [0.1, 0.1, 0.8, 0.8]
when adding Axes, so the blue Axes are placed like the figure below.
In addition of the second Axes, it is specified as [left, bottom, width, height] = [0.6, 0.2, 0.4, 0.3]
, so the green Axes are place as follows.
is_colab = 'google.colab' in str(get_ipython()) # for Google Colab
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2, 3, 10)
y1 = x
y2 = x ** 2
fig = plt.figure(figsize=(5, 4))
ax = fig.add_axes([0.1, 0.1, 0.8, 0.8])
ax.scatter(x, y1)
ax2 = fig.add_axes([0.6, 0.2, 0.4, 0.3])
ax2.scatter(x, y2)
plt.show()