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

2024-04-18 14:38:49 发布

您现在位置:Python中文网/ 问答频道 /正文


Tags: python
3条回答

figure告诉您调用签名:

from matplotlib.pyplot import figure
figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')

figure(figsize=(1,1))将创建一个逐英寸的图像,它将是80×80像素,除非您还提供了不同的dpi参数。

如果已经创建了图形,则可以快速执行以下操作:

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)

Deprecation note:
As per the official Matplotlib guide, usage of the pylab module is no longer recommended. Please consider using the matplotlib.pyplot module instead, as described by this other answer.

以下方法似乎有效:

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

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

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

相关问题 更多 >