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

Attachment 'MPL_size_test.py'

Download

   1 #!/usr/bin/env python2.4
   2 
   3 """
   4 This is a small demo file that helps teach how to adjust figure sizes
   5 for matplotlib
   6 
   7 First a little introduction:
   8 
   9 There are three parameters define an image size (this is not MPL specific):
  10 
  11 Size in length units (inches, cm, pt, etc): i.e. 5"x7"
  12 Size in pixels: i.e. 800x600 pixels
  13 Dots per inch (dpi) i.e. 100 dpi
  14 
  15 Only two of these are independent, so if you define two of them, the
  16 third can be calculated from the others.
  17 
  18 When displaying on a computer screen (or saved to a PNG), the size in
  19 length units is irrelevant, the pixels are simply displayed. When
  20 printed, or saved to PS, EPS or PDF (all designed to support printing),
  21 then the Size or dpi is used to determine how to scale the image.
  22 
  23 Now I'm getting into how MPL works:
  24 
  25 1) The size of a figure is defined in length units (inches), and can be
  26 set by:
  27 
  28 Figure.set_figsize_inches( (w,h) )
  29 
  30 2) The layout of the figure is defined in 'figure units' so that as the
  31 figure size is changed, the layout (eg axes positions) will update.
  32 
  33 3) Size of text, width of lines, etc is defined in terms of length units
  34 (points?).
  35 
  36 4) When displaying to the screen, or creating an image (PNG) the pixel
  37 size of text and line widths, etc is determined by the dpi setting,
  38 which is set by:
  39 
  40 Figure.set_dpi( val )
  41 
  42 The trick here is that when printing, it's natural to think in terms of
  43 inches, but when creating an image (for a web page, for instance), it is
  44 natural to think in terms of pixel size. However, as of 0.84, pixel size
  45 can only be set directly in the GTK* back-ends, with the
  46 canvas.resize(w,h) method. (remember that you can only set two of the
  47 three size parameters, the third must be calculated from the other
  48 two).
  49 
  50 Another trick:
  51 
  52 Figure.savefig() overrides the ppi setting in figure, and uses a default
  53 (which on my system at least is 100ppi). I you want to overide it, you
  54 can specify the ppi in the savefig call:
  55 
  56 Figure.savefig(filename, ppi=value)
  57 
  58 The following code will hopefully make this more clear, at least for
  59 generating PNGs for web pages and the like.
  60 
  61 """
  62 
  63 import matplotlib
  64 print "using MPL version:", matplotlib.__version__
  65 matplotlib.use("WXAgg") # do this before pylab so you don'tget the default back end.
  66 
  67 import pylab
  68 import matplotlib.numerix as N
  69 
  70 # Generate and plot some simple data:
  71 x = N.arange(0, 2*N.pi, 0.1)
  72 y = N.sin(x)
  73 
  74 pylab.plot(x,y)
  75 F = pylab.gcf()
  76 
  77 # Now check everything with the defaults:
  78 DPI = F.get_dpi()
  79 print "DPI:", DPI
  80 DefaultSize = F.get_size_inches()
  81 print "Default size in Inches", DefaultSize
  82 print "Which should result in a %i x %i Image"%(DPI*DefaultSize[0], DPI*DefaultSize[1])
  83 # the default is 100dpi for savefig:
  84 F.savefig("test1.png")
  85 # this gives me a 797 x 566 pixel image, which is about 100 DPI
  86 
  87 # Now make the image twice as big, while keeping the fonts and all the
  88 # same size
  89 F.set_figsize_inches( (DefaultSize[0]*2, DefaultSize[1]*2) )
  90 Size = F.get_size_inches()
  91 print "Size in Inches", Size
  92 F.savefig("test2.png")
  93 # this results in a 1595x1132 image
  94 
  95 # Now make the image twice as big, making all the fonts and lines
  96 # bigger too.
  97 
  98 F.set_figsize_inches( DefaultSize )# resetthe size
  99 Size = F.get_size_inches()
 100 print "Size in Inches", Size
 101 F.savefig("test3.png", dpi = (200)) # change the dpi
 102 # this also results in a 1595x1132 image, but the fonts are larger.

New Attachment

File to upload
Rename to
Overwrite existing attachment of same name

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.