This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

This is a small demo file that helps teach how to adjust figure sizes for matplotlib

First a little introduction

There are three parameters define an image size (this is not MPL specific):

Only two of these are independent, so if you define two of them, the third can be calculated from the others.

When displaying on a computer screen (or saved to a PNG), the size in length units is irrelevant, the pixels are simply displayed. When printed, or saved to PS, EPS or PDF (all designed to support printing), then the Size or dpi is used to determine how to scale the image.

Now I'm getting into how MPL works

The trick here is that when printing, it's natural to think in terms of inches, but when creating an image (for a web page, for instance), it is natural to think in terms of pixel size. However, as of 0.84, pixel size can only be set directly in the GTK* back-ends, with the canvas.resize(w,h) method. (remember that you can only set two of the three size parameters, the third must be calculated from the other two).

Another trick

Figure.savefig() overrides the dpi setting in figure, and uses a default (which on my system at least is 100 dpi). If you want to overide it, you can specify the 'dpi' in the savefig call: Figure.savefig(filename, dpi=value)

The following code will hopefully make this more clear, at least for generating PNGs for web pages and the like.

MPL_size_test.py

   1 """
   2 This is a small demo file that helps teach how to adjust figure sizes
   3 for matplotlib
   4 
   5 """
   6 
   7 import matplotlib
   8 print "using MPL version:", matplotlib.__version__
   9 matplotlib.use("WXAgg") # do this before pylab so you don'tget the default back end.
  10 
  11 import pylab
  12 import matplotlib.numerix as N
  13 
  14 # Generate and plot some simple data:
  15 x = N.arange(0, 2*N.pi, 0.1)
  16 y = N.sin(x)
  17 
  18 pylab.plot(x,y)
  19 F = pylab.gcf()
  20 
  21 # Now check everything with the defaults:
  22 DPI = F.get_dpi()
  23 print "DPI:", DPI
  24 DefaultSize = F.get_size_inches()
  25 print "Default size in Inches", DefaultSize
  26 print "Which should result in a %i x %i Image"%(DPI*DefaultSize[0], DPI*DefaultSize[1])
  27 # the default is 100dpi for savefig:
  28 F.savefig("test1.png")
  29 # this gives me a 797 x 566 pixel image, which is about 100 DPI
  30 
  31 # Now make the image twice as big, while keeping the fonts and all the
  32 # same size
  33 F.set_figsize_inches( (DefaultSize[0]*2, DefaultSize[1]*2) )
  34 Size = F.get_size_inches()
  35 print "Size in Inches", Size
  36 F.savefig("test2.png")
  37 # this results in a 1595x1132 image
  38 
  39 # Now make the image twice as big, making all the fonts and lines
  40 # bigger too.
  41 
  42 F.set_figsize_inches( DefaultSize )# resetthe size
  43 Size = F.get_size_inches()
  44 print "Size in Inches", Size
  45 F.savefig("test3.png", dpi = (200)) # change the dpi
  46 # this also results in a 1595x1132 image, but the fonts are larger.

Putting more than one image in a figure

Suppose you have two images: 100x100 and 100x50 that you want to display in a figure with a buffer of 20 pixels (relative to image pixels) between them and a border of 10 pixels all around.

The solution isn't particularly object oriented, but at least it gets to the practical details.

   1 def _calcsize(matrix1, matrix2, top=10, left=10, right=10, bottom=10, buffer=20, height=4, scale = 1.):
   2    size1 = array(matrix1.shape) * scale
   3    size2 = array(matrix2.shape) * scale
   4    _width = float(size1[1] + size2[1] + left + right + buffer)
   5    _height = float(max(size1[0], size2[0]) + top + bottom)
   6    x1 = left / _width
   7    y1 = bottom / _height
   8    dx1 = size1[1] / _width
   9    dy1 = size1[0] / _height
  10    size1 = (x1, y1, dx1, dy1)
  11    x2 = (size1[1] + left + buffer) / _width
  12    y2 = bottom / _height
  13    dx2 = size2[1] / _width
  14    dy2 = size2[0] / _height
  15    size2 = (x2, y2, dx2, dy2)
  16    figure = pylab.figure(figsize=(_width * height / _height, height))
  17    axis1 = apply(pylab.axes, size1)
  18    pylab.imshow(X1, aspect='preserve')
  19    axis2 = apply(pylab.axes, size2)
  20    pylab.imshow(X2, aspect='preserve')
  21    return axes1, axes2, figure


SciPy: Cookbook/Matplotlib/AdjustingImageSize (last edited 2015-10-24 17:48:26 by anonymous)