white area around plots

Hi all,

how can I reduce the white area around the plots (several subplots) with legends outside to the right of the axes when saving it to a png-file?

pyplot.savefig('test2.png',bbox_inches='tight') shrinks the paper to axes size but cuts the legend.

Thanks in advance

Mario Mech


Dr. Mario Mech

Institute for Geophysics and Meteorology
University of Cologne
Zuelpicher Str. 49a
50674 Cologne

t: +49 (0)221 - 470 - 1776
f: +49 (0)221 - 470 - 5198
e: mech@...2927...
w: http://www.meteo.uni-koeln.de/~mmech/

The svn version has a new keyword "bbox_extra_artists", which could be used.
But, there is no easy way for the released version of matplotlib.

Below is a workaround you may use, but it's a bit complicated.



fig = figure(1)
ax = fig.add_subplot(111)
l1, = ax.plot([1,2,3])
leg = figlegend([l1], ["test"], 1)

def get_tightbbox(renderer):
    from matplotlib.transforms import Bbox, TransformedBbox, Affine2D

    bb = []

    # add fig legend

    for ax in fig.axes:
        if ax.get_visible():

    _bbox = Bbox.union([b for b in bb if b.width!=0 or b.height!=0])

    bbox_inches = TransformedBbox(_bbox,

    return bbox_inches

fig.get_tightbbox = get_tightbbox