35

The following sample code will produce a basic line plot with no axes and save it as an SVG file:

import matplotlib.pyplot as plt
plt.axis('off')
plt.plot([1,3,1,2,3])
plt.plot([3,1,1,2,1])
plt.savefig("out.svg", transparent = True)

How do I set the resolution / dimensions of the image? There is padding on all sides of the image beyond the line graph. How do I remove the padding so that the lines appear on the edge of the image?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
oden
  • 3,461
  • 1
  • 31
  • 33

3 Answers3

62

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().

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
  • 4
    is there a way to put these in matplotlibrc? `Bad key "savefig.bbox_inches"` – endolith Jul 08 '12 at 00:59
  • You're most welcome. I do not know if it is possible to provide such configuration specifications using a matplotlibrc file. – mechanical_meat Jan 25 '13 at 16:49
  • One other command I like to use (in conjunction with those listed above) from pyplot is plt.tight_layout(), which removes excess white space around the figure. – Blink Mar 04 '14 at 16:23
  • And if I want to set as resolution in pixel for a png? – marco Apr 21 '16 at 12:24
  • as for the style, see https://matplotlib.org/stable/tutorials/introductory/customizing.html: for "tight" use `savefig.bbox: tight`, for padding use `savefig.pad_inches: 0`. – settwi Jul 08 '22 at 13:43
3

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.

Jouni K. Seppänen
  • 43,139
  • 5
  • 71
  • 100
  • Correct. You can adjust the position of the axes in the figure by making the axes manually. The pyplot (or pylab) command for making an axes includes this in its docstring: axes(rect, axisbg='w') where rect=[left, bottom, width, height] in normalized (0,1) units. axisbg is the background color for the axis, default white – timbo Jan 16 '11 at 03:55
2

A very easy way to trim down most padding is to call tight_layout() before saving the figure.

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 10, 200)

fig, ax = plt.subplots()
ax.plot(x, np.sin(x))

fig.tight_layout()
fig.savefig('plot.pdf')
Onno Eberhard
  • 1,341
  • 1
  • 10
  • 18
  • plt.tight_layout() also did the job for me for SVGs. For PNGs, it wasn't necessary but somehow matplotlib seem to treat SVGs differently. – Martin Bucher Jan 31 '22 at 17:16