1

I have a figure saved as .png file by matplotlib of a matrix x:

fig = plt.figure(figsize=(1,1), dpi=100)
plt.imshow(x)
fig.savefig('filename.png')

This is the result, opened in Preview:

enter image description here

There is a white margin that I do not need, an my question is:

  • how can I save the image in python without this margin?

P.S.: If I try the different solutions found in other posts, I don't get the result I need. For example the solutions:

fig.savefig('filename.png', bbox_inches='tight')

or

fig.savefig('filename.png', bbox_inches='tight', pad_inches=0)

give

enter image description here

giotto
  • 532
  • 3
  • 15

1 Answers1

1

use bbox_inches='tight' in savefig:

fig.savefig('filename.png', bbox_inches='tight')
onno
  • 969
  • 5
  • 9