如何更改使用Matplotlib绘制的图形的大小?


当前回答

这将在绘制图形后立即调整图形大小(至少使用Qt4Agg/TkAgg,但不使用Mac OS X和Matplotlib 1.4.0):

matplotlib.pyplot.get_current_fig_manager().resize(width_px, height_px)

其他回答

这对我来说很好:

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

此论坛帖子也可能有帮助:调整图形窗口的大小

如果已经创建了地物,可以使用figure.set_size_inches调整地物大小:

fig = matplotlib.pyplot.gcf()
fig.set_size_inches(18.5, 10.5)
fig.savefig('test2png.png', dpi=100)

要将大小更改传播到现有GUI窗口,请添加forward=True:

fig.set_size_inches(18.5, 10.5, forward=True)

此外,正如Erik Shilts在评论中提到的,您还可以使用figure.set_dpi来“设置图形的分辨率,单位为每英寸点数”

fig.set_dpi(100)

图告诉您通话签名:

from matplotlib.pyplot import figure

figure(figsize=(8, 6), dpi=80)

图(figsize=(1,1))将创建一个一英寸一英寸的图像,除非您也给出了不同的dpi参数,否则它将是80x80像素。

谷歌中“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.]

两个注意事项:

模块注释和实际输出不同。这个答案允许轻松地将所有三个图像组合在一个图像文件中,以查看大小的差异。

使用plt.rcParams

如果您想在不使用地物环境的情况下更改大小,也可以使用此解决方法。例如,如果使用plt.plot(),可以设置一个具有宽度和高度的元组。

import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,3)

这在内联绘图时非常有用(例如,使用IPython Notebook)。正如我们所注意到的,最好不要将此语句放在imports语句的同一单元格中。

要将后续绘图的全局地物尺寸重置回默认值,请执行以下操作:

plt.rcParams["figure.figsize"] = plt.rcParamsDefault["figure.figsize"]

转换为厘米

figsize元组接受英寸,因此如果要将其设置为厘米,则必须将其除以2.54。看看这个问题。