%matplotlib inline
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()
from matplotlib import pyplot as plt
F = 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
#!/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.]
13 回答
尝试注释掉
fig = ...
行如果您正在寻找一种方法来改变Pandas中的数字大小,您可以这样做:
其中
df
是Pandas数据帧 . 如果要更改默认设置,可以执行以下操作:请尝试以下简单代码:
您需要在绘图前设置图形大小 .
你可以简单地使用(来自matplotlib.figure.Figure):
从Matplotlib 2.0.0开始,对画布的更改将立即显示为
forward
关键字defaults to True .如果您只想change the width or height而不是两者,您可以使用
fig.set_figwidth(val)
或fig.set_figheight(val)
这些也会立即更新您的画布,但仅限于Matplotlib 2.2.0和更新版本 .
适用于旧版本
您需要明确指定
forward=True
才能在早于上面指定版本的版本中实时更新画布 . 请注意,set_figwidth
和set_figheight
函数不支持早于Matplotlib 1.5.0的版本中的forward
参数 .由于Matplotlib isn't able本身使用公制系统,如果要以合理的长度单位(例如厘米)指定图形的大小,则可以执行以下操作(代码来自gns-ank):
然后你可以使用:
如果您已经创建了图形,则可以快速执行此操作:
要将大小更改传播到现有gui窗口,请添加
forward=True
figure告诉你呼叫签名:
figure(figsize=(1,1))
将创建一个逐英寸的图像,除非你也提供不同的dpi参数,否则它将是80×80像素 .以下似乎有效:
这使得图形的宽度为5英寸,高度为10 inches .
然后,Figure类将其用作其中一个参数的默认值 .
即使在绘制图形之后,这也会立即调整大小(至少使用Qt4Agg / TkAgg - 但不是MacOSX - 使用matplotlib 1.4.0):
使用plt.rcParams
如果您想在不使用图形环境的情况下更改大小,还可以使用此解决方法 . 因此,如果你使用plt.plot(),你可以设置一个宽度和高度的元组 .
当你内联绘图时(例如使用IPython Notebook),这非常有用 . 正如@asamaier注意到你最好不要将这个语句放在imports语句的同一个单元格中 .
转换为cm
figsize
元组接受英寸,所以如果你想把它设置为厘米,你必须将它们除以2.54,看看this question .这适合我:
这也可能会有所帮助:http://matplotlib.1069221.n5.nabble.com/Resizing-figure-windows-td11424.html
要增加图形的大小N次,您需要在pl.show()之前插入它:
它也适用于ipython笔记本 .
Google中
'matplotlib figure size'
的第一个链接是AdjustingImageSize(Google cache of the page) .这是上一页的测试脚本 . 它会创建相同图像的不同大小的
test[1-3].png
文件:输出:
两个说明:
模块注释和实际输出不同 .
This answer允许轻松地将所有三个图像组合在一个图像文件中,以查看大小的差异 .