13

I have a problem which is similar to the one posted here. The difference is that I get unwanted white spaces inside the plot area when I plot two subplots which share axes via the sharex and sharey attributes. The white spaces persist even after setting autoscale(False). For example, using similar code as in the answer to the post mentioned above:

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
ax = fig.add_subplot(2, 1, 1)
ax.imshow(np.random.random((10,10)))
ax.autoscale(False)
ax2 = fig.add_subplot(2, 1, 2, sharex=ax, sharey=ax)   # adding sharex and sharey
ax2.imshow(np.random.random((10,10)))
ax2.autoscale(False)
plt.show()

results in this image.

I have also tried ax.set_xlim(0, 10) and ax.set_xbound(0, 10) as per suggestions here, but to no avail. How can I get rid of the extra white spaces? Any ideas would be appreciated.

Community
  • 1
  • 1
mlynn
  • 861
  • 2
  • 8
  • 11

3 Answers3

17

As suggested here, adding:

ax.set_adjustable('box-forced')
ax2.set_adjustable('box-forced')

solves the problem.

(documentation)

tacaswell
  • 84,579
  • 22
  • 210
  • 199
mlynn
  • 861
  • 2
  • 8
  • 11
  • 1
    This is helpful, but I would add another way, if you don't mind losing the aspect ratio and want to fill all the available axes space: use `aspect='auto'` in `imshow()`. – PlasmaBinturong Jan 11 '18 at 09:50
3

Using plt.subplots as:

fig, ax = plt.subplots(nrows=2, ncols=1, sharex=True, sharey=False)
ax[0].imshow(np.random.random((10,10)))
ax[0].autoscale(False)
ax[1].imshow(np.random.random((10,10)))
ax[1].autoscale(False)

I get this figure with no white spaces within axes. Using figsize within plt.subplots or fig.subplots_adjust you can get better axis ratios.

Francesco Montesano
  • 8,485
  • 2
  • 40
  • 64
  • Thanks. However, I would like a solution where the axes' ranges do not change with resizing of the figure. – mlynn Feb 27 '13 at 07:59
  • @mlynn: what do you mean? do you want your axes to be always the same absolute size (like 5x5 cm)? or you want them to maintain the same aspect ratio independently of the figure size? – Francesco Montesano Feb 27 '13 at 09:07
  • What I would like is for each subplot to display the full dimensions of the image (in this case, 10x10 pixels), maintaining the same aspect ratio independently of the figure size. Basically, if I do not add the `sharex` and `sharey` options in my original code above, I get the plots I want. I can resize the figure, and each subplot would resize accordingly. The reason I would like the axes to be shared is that when I zoom in on one subplot, I would like the other subplot to zoom in correspondingly. – mlynn Feb 27 '13 at 20:24
1

The issue is the helpful machinery from using add_subplot. Notice that the amount of white space changes if you resize the figure.

The following seems to work (until you re-size the figure)

import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure(figsize=(5, 5))
ax = fig.add_axes([.3, .55, .35, .35]) 
ax.imshow(np.random.random((10,10)))
ax.autoscale(False)
ax2 = fig.add_axes([.3,  .05, .35, .35], sharex=ax, sharey=ax ) 
ax2.imshow(np.random.random((10,10)))
ax2.autoscale(False)

plt.show()

This looks like a bad interaction between the size/location of the axes object, the shared axes, and the equal aspect ratio from imshow.

If you can live with out the ticks, you can do

ax.set_axis_off()
ax2.set_axis_off()

I think it is worth opening an issue on the matplotlib github for this.

tacaswell
  • 84,579
  • 22
  • 210
  • 199
  • 1
    Setting `aspect=auto` would get rid of the white spaces in this case, but this is not always ideal. I opened an issue on the matplotlib github as you suggested -- thanks. – mlynn Feb 27 '13 at 07:52