#!/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.]
%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
图告诉你调用签名。
figure(figsize=(1,1))
将创建一个英寸的图像,也就是80×80像素,除非你还给出一个不同的dpi参数。如果你'已经创建了人物,你可以快速地做到这一点。
要将尺寸变化传播到现有的gui窗口,添加
forward=True
。下面的方法似乎可以工作。
这使得该图的宽度为5英寸,高度为10英寸。
然后,Figure类将此作为其一个参数的默认值。
使用 plt.rcParams
如果你想在不使用图形环境的情况下改变尺寸,也有这种变通方法。 例如,如果你使用[
plt.plot()
][1],你可以设置一个包含宽度和高度的元组。当你进行内联绘图时,这个功能非常有用(例如,使用IPython Notebook)。 IPython Notebook)。) 正如 @asamaier 所注意到的那样,最好不要把这个语句放在导入语句的同一个单元格中。
转换为cm
figsize
元组接受的是英寸,所以如果你想以厘米为单位设置,就必须将它们除以2.54,请看[本题][2]。[1]: https://matplotlib.org/api/_as_gen/matplotlib.pyplot.figure.html [2]: https://stackoverflow.com/questions/14708695/specify-figure-size-in-centimeter-in-matplotlib
请试着写一个简单的代码,如下。
绘制之前需要设置好数字的大小。
在谷歌中,"'matplotlib数字大小' "的第一个链接是AdjustingImageSize([谷歌缓存页面](https://webcache.googleusercontent.com/search?q=cache:5oqjjm8c8UMJ:https://scipy.github.io/old-wiki/pages/Cookbook/Matplotlib/AdjustingImageSize.html+&cd=2&hl=en&ct=clnk&gl=fr))。
这里是上述页面的测试脚本。它创建了同一图片的不同尺寸的
test[1-3].png
文件。输出。
两个音符。
1.模块的注释和实际输出不同。
2.这个答案可以很容易地将所有三张图片合并到一个图片文件中,以查看尺寸的差异。
如果您正在寻找在Pandas中改变人物尺寸的方法,您可以这样做,例如:
。
其中
df
是一个Pandas数据框架。 或者,使用现有的图形或轴如果您想更改默认设置,可以执行以下操作。
您可以简单地使用(来自[matplotlib.figure.Figure][1])。
从Matplotlib 2.0.0开始,对画布的更改将立即可见,因为
forward
关键字[默认为True
][2]。如果您只想[改变宽度或高度][3],而不是同时改变宽度和高度,您可以使用
fig.set_figwidth(val)
或fig.set_figheight(val)
。这些也会立即更新您的画布,但只在Matplotlib 2.2.0和更新版本中。
对于旧版本
您需要明确指定
forward=True
,以便在比上面指定的版本更老的版本中实时更新您的画布。 请注意,在Matplotlib 1.5.0以上的版本中,"set_figwidth "和 "set_figheight "函数不支持 "forward "参数。[1]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html [2]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=set%20size%20inches#matplotlib.figure.Figure.set_size_inches [3]: https://matplotlib.org/api/_as_gen/matplotlib.figure.Figure.html?highlight=set%20size%20inches#matplotlib.figure.Figure.set_figheight [4]: https://github.com/matplotlib/matplotlib/issues/9669
试着把 "fig = ... "这一行注释掉。
您也可以使用。
如果要将数字的大小增加N倍,你需要在pl.show()之前插入这个。
它也能很好地与ipython笔记本配合使用。
这对我来说很有效。
这可能也会有帮助。 http://matplotlib.1069221.n5.nabble.com/Resizing-figure-windows-td11424.html
由于 Matplotlib is&39;t able 本身就使用公制,如果您想用合理的长度单位(如厘米)来指定数字的大小,您可以执行以下操作(代码来自 gns-ank)。
那么你可以用。
这个功能可以在图形绘制完成后立即调整图形的大小(至少在使用Qt4Agg/TkAgg时是这样,但不是MacOSX,而是使用matplotlib 1.4.0)。
另一个选择,使用matplotlib中的rc()函数(单位是英寸)