1
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")
66
67 import pylab
68 import matplotlib.numerix as N
69
70
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
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
84 F.savefig("test1.png")
85
86
87
88
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
94
95
96
97
98 F.set_figsize_inches( DefaultSize )
99 Size = F.get_size_inches()
100 print "Size in Inches", Size
101 F.savefig("test3.png", dpi = (200))
102
, as shown below in the list of files. Do
link, since this is subject to change and can break easily.