1

I would like to use plt.imshow without a large white margin around the image. Here is an example:

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(8, 8)
plt.imshow(data, origin='lower', interpolation='None', aspect='equal')
plt.axis('off')
plt.show()

and viewer window produced:

enter image description here

How to reduce the white space margin around the image?

This is not the same question as matplotlib.pyplot.imshow: removing white space within plots when using attributes "sharex" and "sharey". I tried suggestions from there to no effect.

2
  • 3
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0). See e.g. this post Commented Feb 16, 2022 at 21:46
  • @JohanC Great! This actually fills all available window pane. If you post it as an answer, I will accept it as a solution. Commented Feb 16, 2022 at 21:51

1 Answer 1

3

Using matplotlib.pyplot.tight_layout() may solve your problem.

import matplotlib.pyplot as plt
import numpy as np

data = np.random.rand(8, 8)
plt.imshow(data, origin='lower', interpolation='None', aspect='equal')
plt.axis('off')
plt.tight_layout()
plt.show()

tight layout exemple

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, it works nicely. The comment above with plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0) fills the window even better.
The command given by @PaulJurczak is the actual solution. I was able to get rid of white background only with this command. Thank you very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.