Friday, March 4, 2011

How do you change the size of figures drawn with matplotlib?

How do you change the size of figure drawn with matplotlib?

From stackoverflow
  • The following seems to work:

    from pylab import *
    rcParams['figure.figsize'] = 5, 10
    

    This makes the figure's width 5 inches, and its height 10 inches.

    The Figure class then uses this as the default value for one of its arguments.


    Does anyone know more direct mechanisms?

  • The first link in Google for 'matplotlib figure size' is AdjustingImageSize (google cache of the page: http://74.125.77.132/search?q=cache:http://www.scipy.org/Cookbook/Matplotlib/AdjustingImageSize).

    Here's a test script from the above page. It creates test[1-3].png files of different sizes of the same image:

    #!/usr/bin/env python
    """
    This is a small demo file that helps teach how to adjust figure sizes
    for matplotlib
    
    """
    
    import matplotlib
    print "using MPL version:", matplotlib.__version__
    matplotlib.use("WXAgg") # do this before pylab so you don'tget the default back end.
    
    import pylab
    import matplotlib.numerix as N
    
    # Generate and plot some simple data:
    x = N.arange(0, 2*N.pi, 0.1)
    y = N.sin(x)
    
    pylab.plot(x,y)
    F = pylab.gcf()
    
    # Now check everything with the defaults:
    DPI = F.get_dpi()
    print "DPI:", DPI
    DefaultSize = F.get_size_inches()
    print "Default size in Inches", DefaultSize
    print "Which should result in a %i x %i Image"%(DPI*DefaultSize[0], DPI*DefaultSize[1])
    # the default is 100dpi for savefig:
    F.savefig("test1.png")
    # this gives me a 797 x 566 pixel image, which is about 100 DPI
    
    # Now make the image twice as big, while keeping the fonts and all the
    # same size
    F.set_size_inches( (DefaultSize[0]*2, DefaultSize[1]*2) )
    Size = F.get_size_inches()
    print "Size in Inches", Size
    F.savefig("test2.png")
    # this results in a 1595x1132 image
    
    # Now make the image twice as big, making all the fonts and lines
    # bigger too.
    
    F.set_size_inches( DefaultSize )# resetthe size
    Size = F.get_size_inches()
    print "Size in Inches", Size
    F.savefig("test3.png", dpi = (200)) # change the dpi
    # this also results in a 1595x1132 image, but the fonts are larger.
    

    Output:

    using MPL version: 0.98.1
    DPI: 80
    Default size in Inches [ 8.  6.]
    Which should result in a 640 x 480 Image
    Size in Inches [ 16.  12.]
    Size in Inches [ 16.  12.]
    

    Two notes:

    1. The module comments and the actual output differ.

    2. This answer allows easily to combine all three images in one image file to see the difference in sizes.

  • Hmm, I appear to be unable to use google...

  • help(figure) tells you the call signature:

    figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
    

    So figure(figsize=(1,1)) creates an inch-by-inch image, which will be 80-by-80 pixels unless you also give a different dpi argument.

0 comments:

Post a Comment