Matplotlib savefig image trim

I am continually amazed at how many ways there are to do the same thing in matplotlib.
As such, I am sure that someone can make this code much more terse.
At any rate, this should clearly demonstrate how to go about solving your problem.

>>> import pylab
>>> fig = pylab.figure()

>>> pylab.axis('off')
(0.0, 1.0, 0.0, 1.0)
>>> pylab.plot([1,3,1,2,3])
[<matplotlib.lines.Line2D object at 0x37d8cd0>]
>>> pylab.plot([3,1,1,2,1])
[<matplotlib.lines.Line2D object at 0x37d8d10>]

>>> fig.get_size_inches()    # check default size (width, height)
array([ 8.,  6.])
>>> fig.set_size_inches(4,3) 
>>> fig.get_dpi()            # check default dpi (in inches)
80
>>> fig.set_dpi(40)

# using bbox_inches='tight' and pad_inches=0 
# I managed to remove most of the padding; 
# but a small amount still persists
>>> fig.savefig('out.svg', transparent=True, bbox_inches='tight', pad_inches=0)

Documentation for savefig().


The default axis object leaves some room for titles, tick labels and the like. Make your own axis object that fills the whole area:

fig=figure()
ax=fig.add_axes((0,0,1,1))
ax.set_axis_off()
ax.plot([3,1,1,2,1])
ax.plot([1,3,1,2,3])
fig.savefig('out.svg')

In svg format I can't see the line that's right at the bottom, but in png format I can, so it's probably a feature of the svg renderer. You might want to add just a little padding to keep everything visible.