slothfulwave612
slothfulwave612

Reputation: 1409

Matplotlib: How to use imshow on whole plot?

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?

Upvotes: 1

Views: 478

Answers (1)

Stef
Stef

Reputation: 30679

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.

Upvotes: 2

Related Questions