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


当前回答

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

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

其他回答

我总是使用以下模式:

x_inches = 150*(1/25.4)     # [mm]*constant
y_inches = x_inches*(0.8)
dpi = 96

fig = plt.figure(1, figsize = (x_inches,y_inches), dpi = dpi, constrained_layout = True)

使用此示例,您可以设置以英寸或毫米为单位的图形尺寸。将constrained_layout设置为True时,绘图将填充图形而无边框。

这对我来说很好:

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

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

试着注释一下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)

折旧说明:根据Matplotlib官方指南,不再建议使用pylab模块。请考虑改用matplotlib.pyplot模块,如另一个答案所述。

以下方法似乎有效:

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

这使图形的宽度为5英寸,高度为10英寸。

然后,Figure类将其用作其参数之一的默认值。