Skip to content Skip to sidebar Skip to footer

How To Make Matplotlib.pyplot Subplots That Overlap?

I am learning how to use subplots. For example: import numpy import matplotlib.pyplot as plt plt.figure(1) plt.subplot(221) plt.subplot(222) plt.subplot(223) plt.show() plt.clos

Solution 1:

If you want a total control of the subplots size and position, use Matplotlib add_axes method instead.

import matplotlib.pyplot as plt

fig = plt.figure(figsize=(6, 4))

ax1 = fig.add_axes([0.1, 0.1, 0.85, 0.85])
ax2 = fig.add_axes([0.4, 0.6, 0.45, 0.3])
ax3 = fig.add_axes([0.6, 0.2, 0.2, 0.65])

ax1.text(0.01, 0.95, "ax1", size=12)
ax2.text(0.05, 0.8, "ax2", size=12)
ax3.text(0.05, 0.9, "ax3", size=12)

plt.show()

Imgur

Solution 2:

You can use mpl_toolkits.axes_grid1.inset_locator.inset_axes to create an inset axes on an existing figure. I added a print statement at the end which shows a list of two axes.

import matplotlib.pyplot as plt
import mpl_toolkits.axes_grid1.inset_locator as mpl_il

plt.plot() 

ax2 = mpl_il.inset_axes(plt.gca(), width='60%', height='40%', loc=6)
ax2.plot()

print(plt.gcf().get_axes())
plt.show()

Creating inset axes on existing figure

Solution 3:

It's not possible to use plt.subplots() to create overlapping subplots. Also, plt.subplot2grid will not work.

However, you can create them using the figure.add_subplot() method.

import matplotlib.pyplot as pltfig= plt.figure(1)
fig.add_subplot(111)
fig.add_subplot(222)
fig.add_subplot(223)

plt.show()

enter image description here

Post a Comment for "How To Make Matplotlib.pyplot Subplots That Overlap?"