如何更改使用Matplotlib绘制的图形的大小?
当前回答
概括和简化了psihodelia的答案:
如果要通过因子sizefactor更改地物的当前大小:
import matplotlib.pyplot as plt
# Here goes your code
fig_size = plt.gcf().get_size_inches() # Get current size
sizefactor = 0.8 # Set a zoom factor
# Modify the current size by the factor
plt.gcf().set_size_inches(sizefactor * fig_size)
更改当前大小后,可能需要微调子地块布局。您可以在图形窗口GUI中或通过命令sublots_adjust执行此操作
例如
plt.subplots_adjust(left=0.16, bottom=0.19, top=0.82)
其他回答
折旧说明:根据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.]
两个注意事项:
模块注释和实际输出不同。这个答案允许轻松地将所有三个图像组合在一个图像文件中,以查看大小的差异。
概括和简化了psihodelia的答案:
如果要通过因子sizefactor更改地物的当前大小:
import matplotlib.pyplot as plt
# Here goes your code
fig_size = plt.gcf().get_size_inches() # Get current size
sizefactor = 0.8 # Set a zoom factor
# Modify the current size by the factor
plt.gcf().set_size_inches(sizefactor * fig_size)
更改当前大小后,可能需要微调子地块布局。您可以在图形窗口GUI中或通过命令sublots_adjust执行此操作
例如
plt.subplots_adjust(left=0.16, bottom=0.19, top=0.82)
这对我来说很好:
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
此论坛帖子也可能有帮助:调整图形窗口的大小
我总是使用以下模式:
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时,绘图将填充图形而无边框。
推荐文章
- 滚动或滑动窗口迭代器?
- python的方法找到最大值和它的索引在一个列表?
- 如何读取文件的前N行?
- 如何删除matplotlib中的顶部和右侧轴?
- 解析.py文件,读取AST,修改它,然后写回修改后的源代码
- Visual Studio Code:如何调试Python脚本的参数
- 使用元组/列表等等。从输入vs直接引用类型如list/tuple/etc
- 结合conda环境。Yml和PIP requirements.txt
- 将命名元组转换为字典
- 如何使x轴和y轴的刻度相等呢?
- Numpy在这里函数多个条件
- 在Python中,使用argparse只允许正整数
- 如何排序mongodb与pymongo
- 不可变与可变类型
- 列表是线程安全的吗?