-1

Trying to change the size of a plot. But somewhere happens a leak and image instead of being 1000x1000 is 775x770 px. I know that pad tightens the figure. I expect the figure be of 1000x1000 without edges, borders, etc

w = 1000
    h = 1000

    plt.figure(figsize=(w/1000,h/1000), dpi=100)
    sn.heatmap(depthnp,  xticklabels=False, yticklabels=False,
               center=default, cmap=self.cmap, cbar=False)
    plt.savefig('tempFile.png', dpi=1000, bbox_inches='tight',pad_inches = 0)

Is there a way to make it fit the expected size?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Karin Moe
  • 21
  • 5
  • Don't add `bbox_inches='tight'` which is designed to expand or crop the figure size to a tight fit around the plot. – mwaskom Mar 25 '22 at 21:36
  • Does this answer your question? [Specifying and saving a figure with exact size in pixels](https://stackoverflow.com/questions/13714454/specifying-and-saving-a-figure-with-exact-size-in-pixels) – t9dupuy Mar 25 '22 at 21:43
  • @mwaskom I don't need edges, but a figure itself – Karin Moe Mar 25 '22 at 22:14
  • @TIIITAN that's what i used as example, but no – Karin Moe Mar 25 '22 at 22:27

1 Answers1

1

plt.subplots_adjust(left=0, bottom=0, right=1, top=1) would reduce the whitespace. plt.axis('off') turns off the axes.

Also note that figsize=(1,1) might give strange results when working with text. figsize=(10,10) and plt.savefig(..., dpi=100) could work better.

import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
from scipy.ndimage import gaussian_filter

w = 1000
h = 1000
plt.figure(figsize=(w / 100, h / 100))
ax = sns.heatmap(gaussian_filter(np.random.rand(50, 50), 5), xticklabels=False, yticklabels=False, cbar=False)
plt.subplots_adjust(left=0, bottom=0, right=1, top=1)
plt.axis('off')
plt.savefig('tempFile.png', dpi=100, pad_inches=0)

1000x1000 bitmap

JohanC
  • 71,591
  • 8
  • 33
  • 66
  • thanx, I've created smth simular. what type of "wierd results" does it create? – Karin Moe Mar 28 '22 at 10:19
  • A strange result would be that the default text wouldn't fit into the plot, it will look extremely large. Often, matplotlib will remove tick marks to try to make things fit. – JohanC Mar 28 '22 at 14:06