如何更改使用Matplotlib绘制的图形的大小?
当前回答
谷歌中“matplotlib图形大小”的第一个链接是调整图像大小(页面的谷歌缓存)。
这是上面页面中的测试脚本。它创建同一图像的不同大小的测试[1-3].png文件:
#!/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 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:", 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.
输出:
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.]
两个注意事项:
模块注释和实际输出不同。这个答案允许轻松地将所有三个图像组合在一个图像文件中,以查看大小的差异。
其他回答
折旧说明:根据Matplotlib官方指南,不再建议使用pylab模块。请考虑改用matplotlib.pyplot模块,如另一个答案所述。
以下方法似乎有效:
from pylab import rcParams
rcParams['figure.figsize'] = 5, 10
这使图形的宽度为5英寸,高度为10英寸。
然后,Figure类将其用作其参数之一的默认值。
谷歌中“matplotlib图形大小”的第一个链接是调整图像大小(页面的谷歌缓存)。
这是上面页面中的测试脚本。它创建同一图像的不同大小的测试[1-3].png文件:
#!/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 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:", 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.
输出:
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.]
两个注意事项:
模块注释和实际输出不同。这个答案允许轻松地将所有三个图像组合在一个图像文件中,以查看大小的差异。
您可以简单地使用(来自matplotlib.figure.figure):
fig.set_size_inches(width,height)
从Matplotlib 2.0.0开始,画布的更改将立即可见,因为forward关键字默认为True。
如果只想更改宽度或高度,而不是同时更改两者,可以使用
图set_figwidth(val)或图set_feight(val)
这些也会立即更新画布,但仅限于Matplotlib 2.2.0及更新版本。
对于旧版本
您需要显式指定forward=True,以便在比上面指定的更早的版本中实时更新画布。注意,在Matplotlib 1.5.0之前的版本中,set_figwidth和set_figeight函数不支持forward参数。
试着注释一下fig=。。。线
import numpy as np
import matplotlib.pyplot as plt
N = 50
x = np.random.rand(N)
y = np.random.rand(N)
area = np.pi * (15 * np.random.rand(N))**2
fig = plt.figure(figsize=(18, 18))
plt.scatter(x, y, s=area, alpha=0.5)
plt.show()
这将在绘制图形后立即调整图形大小(至少使用Qt4Agg/TkAgg,但不使用Mac OS X和Matplotlib 1.4.0):
matplotlib.pyplot.get_current_fig_manager().resize(width_px, height_px)