如何改变用matplotlib绘制的图形的大小?
图告诉你调用签名。
from matplotlib.pyplot import figure
figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k')
figure(figsize=(1,1))
将创建一个英寸的图像,也就是80×80像素,除非你还给出一个不同的dpi参数。
弃用说明: 根据Matplotlib官方指南,不再推荐使用
pylab
模块。请考虑使用matplotlib.pyplot
模块来代替,如这个其他答案所述。
下面的方法似乎可以工作。
from pylab import rcParams
rcParams['figure.figsize'] = 5, 10
这使得该图的宽度为5英寸,高度为10英寸。
然后,Figure类将此作为其一个参数的默认值。
如果你想在不使用图形环境的情况下改变尺寸,也有这种变通方法。
例如,如果你使用[plt.plot()
][1],你可以设置一个包含宽度和高度的元组。
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = (20,3)
当你进行内联绘图时,这个功能非常有用(例如,使用IPython Notebook)。 IPython Notebook)。) 正如 @asamaier 所注意到的那样,最好不要把这个语句放在导入语句的同一个单元格中。
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
文件。
#!/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.]
两个音符。
1.模块的注释和实际输出不同。
2.这个答案可以很容易地将所有三张图片合并到一个图片文件中,以查看尺寸的差异。
如果您正在寻找在Pandas中改变人物尺寸的方法,您可以这样做,例如:
df['some_column'].plot(figsize=(10, 5))
。
df['some_column'].plot(figsize=(10, 5))
其中df
是一个Pandas数据框架。
或者,使用现有的图形或轴
fig, ax = plt.subplots(figsize=(10,5))
df['some_column'].plot(ax=ax)
如果您想更改默认设置,可以执行以下操作。
import matplotlib
matplotlib.rc('figure', figsize=(10, 5))
您可以简单地使用(来自[matplotlib.figure.Figure][1])。
fig.set_size_inches(width,height)
从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 = ... "这一行注释掉。
%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
这可能也会有帮助。 http://matplotlib.1069221.n5.nabble.com/Resizing-figure-windows-td11424.html
由于 Matplotlib is&39;t able 本身就使用公制,如果您想用合理的长度单位(如厘米)来指定数字的大小,您可以执行以下操作(代码来自 gns-ank)。
def cm2inch(*tupl):
inch = 2.54
if isinstance(tupl[0], tuple):
return tuple(i/inch for i in tupl[0])
else:
return tuple(i/inch for i in tupl)
那么你可以用。
plt.figure(figsize=cm2inch(21, 29.7))