1

The following code

import numpy as np
import matplotlib.pyplot as plt

nrows, ncols = 5, 11

fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(ncols, nrows))
fig.tight_layout()

W, H = 73, 73

for r in range(nrows):
    axes[r,0].imshow(np.random.rand(W, H), cmap="gray", interpolation="none")
    axes[r,0].set_axis_off()
    axes[r,0].set_aspect('equal')

for c in range(ncols-1):
    for r in range(nrows):
        axes[r,c+1].imshow(np.random.rand(W, H), cmap="viridis", interpolation="none")
        axes[r,c+1].set_axis_off()
        axes[r,c+1].set_aspect('equal')

plt.subplots_adjust(wspace=0.1, hspace=0.1)
plt.savefig("test_plot.png", dpi=300, bbox_inches="tight")
plt.close()

produces the following output:

enter image description here

The black background is for reference only. The white outer border is larger on the left and bottom than on the right and top. I would like to know how I can adjust the plot such that the distance to the edge is the same on all sides.

Gilfoyle
  • 3,282
  • 3
  • 47
  • 83
  • Specifically, [this answer](https://stackoverflow.com/a/6541454/4124317). – ImportanceOfBeingErnest Feb 05 '19 at 17:12
  • @ImportanceOfBeingErnest I tried that before posting and it did not work. Where do I place `subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=None, hspace=None)`? and do I still need `bbox_inches="tight"`, `plt.subplots_adjust(wspace=0.1, hspace=0.1)` and `fig.tight_layout()`? – Gilfoyle Feb 05 '19 at 17:22
  • You should of course put actual numbers in, not `None`. Other settings like `tight..` will overwrite those, so don't use both. – ImportanceOfBeingErnest Feb 05 '19 at 17:42

0 Answers0