pythonmatplotlibsavefig

tight savefig without axes in matplotlib


When using a matplotlib to draw something without axes, savefig() isn't truly "tight":

import matplotlib.pyplot as plt

circ = plt.Circle((0, 0), 1.0)
plt.gca().add_artist(circ)
plt.gca().set_aspect("equal")
plt.axis("off")
# plt.show()
plt.savefig("out.svg", bbox_inches="tight")

enter image description here

That's because the SVG contains the hidden "background patch"

  <g id="patch_1">
   <path d="M 0 280.512 
L 280.512 280.512 
L 280.512 0 
L 0 0 
z
" style="fill:none;"/>
  </g>

How to remove it?


Solution

  • pad_inches option!

    plt.savefig("out.svg", bbox_inches="tight", pad_inches=0)