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?

2
  • Does stackoverflow.com/questions/9642053/… answer your question?
    – Thomas
    Commented Feb 10, 2021 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. Commented Feb 10, 2021 at 14:08

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.