I want a grid figure with m rows and n columns to be displayed sharing both x and y axis between subplots, and no whitespace between them. This is easy to achieve with simple plot in matplotlib, as with this code
def plot_me():
n = 4
m = 5
f, axs = plt.subplots(m,n, sharex=True, sharey=True, gridspec_kw=dict(hspace=0, wspace=0))
x = np.arange(44)
for i in range(m):
for j in range(n):
axs[i,j].plot(np.sin(x*(i*j+1)))
but when I change axs[i,j].plot()
to axs[i,j].imshow()
then there is a vertical whitespace between the columns, but not a horizontal one! Why is that?
def imshow_me():
n = 4
m = 5
f, axs = plt.subplots(m,n, sharex=True, sharey=True, gridspec_kw=dict(hspace=0, wspace=0))
for i in range(m):
for j in range(n):
axs[i, j].imshow(np.random.random((100, 100)))
I also tried with f.subplots_adjust(wspace=0, hspace=0)
but with the same result.
And also with mosaic plot, and again in one axis there is no whitespace, while in the other is.
I'm using Python 3.11.2 in visual studio code 1.78.2, matplotlib 3.7.1
Thanks in advance.
EDIT, as with @jared linked thread Matplotlib adjust image subplots hspace and wspace does not raise the specific issue of imshow
compared to simple plot
, nor are the answerd there satisfactory solving the issue.
What solved the issue for me is using pcolormesh
, as per @jared comment. Alternatively, as explained by @gboffi one can force the plot h/w ratio to be exactly 1.5 to avoid whitespaces