#!/usr/bin/env python"""This is a small demo file that helps teach how to adjust figure sizesfor matplotlib
"""
import matplotlibprint "using MPL version:", matplotlib.__version__matplotlib.use("WXAgg") # do this before pylab so you don'tget the default back end.
import pylabimport numpy as np
# Generate and plot some simple data:x = np.arange(0, 2*np.pi, 0.1)y = np.sin(x)
pylab.plot(x,y)F = pylab.gcf()
# Now check everything with the defaults:DPI = F.get_dpi()print "DPI:", DPIDefaultSize = F.get_size_inches()print "Default size in Inches", DefaultSizeprint "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 sizeF.set_size_inches( (DefaultSize[0]*2, DefaultSize[1]*2) )Size = F.get_size_inches()print "Size in Inches", SizeF.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 sizeSize = F.get_size_inches()print "Size in Inches", SizeF.savefig("test3.png", dpi = (200)) # change the dpi# this also results in a 1595x1132 image, but the fonts are larger.
输出:
using MPL version: 0.98.1DPI: 80Default size in Inches [ 8. 6.]Which should result in a 640 x 480 ImageSize in Inches [ 16. 12.]Size in Inches [ 16. 12.]
from matplotlib import pyplot as plt
F = plt.gcf()Size = F.get_size_inches()F.set_size_inches(Size[0]*2, Size[1]*2, forward=True) # Set forward to True to resize window along with plot in figure.plt.show() # Or plt.imshow(z_array) if using an animation, where z_array is a matrix or NumPy array
import matplotlib.pyplot as plt
# Here goes your code
fig_size = plt.gcf().get_size_inches() # Get current sizesizefactor = 0.8 # Set a zoom factor# Modify the current size by the factorplt.gcf().set_size_inches(sizefactor * fig_size)