The problem
I am trying to plot an image next to some data. However, I would like the image to expand to be flush with the plot labels. For example, the following code (using this tutorial image):
# make the incorrect figure
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
fig = plt.figure(figsize=(8,3))
ax_image = plt.subplot(1,2,1)
plt.imshow(mpimg.imread('stinkbug.png'))
plt.subplot(1,2,2)
plt.plot([0,1],[2,3])
plt.ylabel("y")
plt.xlabel("want image flush with bottom of this label")
fig.tight_layout()
ax_image.axis('off')
fig.savefig("incorrect.png")
yields this plot, with extra whitespace:
The hacky attempt at a solution
I would like a plot that doesn't waste whitespace. The following hacky code (in the vein of this SO link) accomplishes this:
# make the correct figure with manually changing the size
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
fig = plt.figure(figsize=(8,3))
ax_image = fig.add_axes([0.02,0,0.45,1.0])
plt.imshow(mpimg.imread('stinkbug.png'))
plt.subplot(1,2,2)
plt.plot([0,1],[2,3])
plt.ylabel("y")
plt.xlabel("want image flush with bottom of this label")
fig.tight_layout()
ax_image.axis('off')
fig.savefig("correct.png")
yielding the following figure:
The question
Is there any way to plot an image flush with other subplots, without having to resort to manual adjustment of figure sizes?