1

I am using the following code to plot an image in matplotlib:

import matplotlib.pyplot as plt
from pylab import Axes

fig, axes = plt.subplots(nrows=2, ncols=2, figsize=(16,12))

ax = Axes(plt.gcf(),[0,0,1,1],yticks=[],xticks=[],frame_on=False)
plt.gcf().delaxes(plt.gca())
plt.gcf().add_axes(ax)

image = plt.imread("../plots/background.jpg")
ax.imshow(image, zorder=2, extent=[0, 100, 0, 100])

for i, ax in enumerate(fig.get_axes()):
    ax.text(0.5, 0.5, f"Subplot {i}", color="black", size=20, zorder=3)

Output: enter image description here

I want to have the image as the background for the whole figure and print text in each subplots? What changes should I make in my code?

slothfulwave612
  • 1,349
  • 9
  • 22
  • Does https://stackoverflow.com/questions/9642053/matplotlib-stretch-image-to-cover-the-whole-figure answer your question? – Thomas Feb 10 '21 at 13:09
  • Hello Thomas, the solution did worked for a single subplot, but while making multiple subplot I don't know what changes should be made to get the required plot. I have edited the question. – slothfulwave612 Feb 10 '21 at 14:08

1 Answers1

2

Set the axes to fill the whole figure with subplots_adjust:

plt.subplots_adjust(0, 0, 1, 1)

Here, 0 and 1 are fractions of figure size.

Stef
  • 28,728
  • 2
  • 24
  • 52