1

I thought switching from ax.set_xlim to ax.set_xbound was gonna solve my problem but I'm still unable to get rid of the white space in my subplots. Even more curiously, ax.set_ybound, when added, behaves as expected, but ax.set_xbound seems to have no effect on my plot.

Below is my code and resulting plot image for plotting 9 random images.

# Generate fake data for plotting
def random_img():
    return np.random.randint(0, 255, (28,28))

images_to_plot = [random_img() for x in range(9)]

# Prep plotting figure space
plt.clf()
fig, axes = plt.subplots(3,3, sharex=True, sharey=True)
plt.style.use('seaborn-muted')

# Plot on subplot axes
for i in range(9):

    # axes (subplot) objects are stored in 2d array, accessed with axes[row,col]
    subplot_row = i//3 
    subplot_col = i%3  
    ax = axes[subplot_row, subplot_col]

    # Plot image on subplot
    ax.imshow(images_to_plot[i], cmap='gray')

    # WHY DOESNT THIS RESTRICT THE BOUNDS OF THE X-AXIS???
    ax.set_xbound([0,28])

plt.tight_layout()
plt.show()

enter image description here

Max Power
  • 8,265
  • 13
  • 50
  • 91
  • Once you use `subplot_kw=dict(adjustable='box-forced', aspect='equal')` in the subplots creation, you can do the xlim or xbound as usual. – ImportanceOfBeingErnest Jun 22 '17 at 16:18
  • thanks a lot for the solution. since the page you linked does not have any mention of `set_xlim`, `set_ylim`, `set_xbound`, or `set_ybound`--which are the usually accepted answer for this type of question---I would think it would be helpful to have the answer here as an answer (with full working code example) instead of a comment. but obviously I'll defer to your judgement here. – Max Power Jun 22 '17 at 16:36
  • can I ask, Ernest, do you know why `set_ybound` behaved as expected without `subplot_kw` but `set_xbound` did not? I found that behavior puzzling and wish I could wrap my head around why that would be. – Max Power Jun 22 '17 at 16:43
  • I think we can leave it as it is. The linked question gives the solution and my comment below this question mentions that you can use xlim or xbound as usual. The question itself will stay up, so if people search for it they'll find it and can also still upvote if they like. Both set_ybound and set_xbound work, but they don't have the effect of shrinking the axes. Try e.g. `ax.set_xbound([0,28]); ax.set_ybound([0,10])`, this will give you the white space in the y direction. – ImportanceOfBeingErnest Jun 22 '17 at 16:52

0 Answers0