matplotlibで描かれた図形の大きさを変えるにはどうしたらいいですか?

matplotlibで描かれた図のサイズを変更するにはどうすればいいですか?

figureでは、コールサインが表示されます。

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

figure(figsize=(1,1))`は、インチ×インチの画像を作成しますが、これは別のdpi引数を与えない限り、80×80ピクセルになります。

解説 (6)

Deprecation note: 公式Matplotlibガイド]1によると,pylabモジュールの使用はもはや推奨されていません.他の回答]2で説明されているように、代わりに matplotlib.pyplot モジュールの使用を検討してください。

以下のようにするとうまくいくようです。

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

これにより、フィギュアの幅は5インチ、高さは10インチとなります。

Figureクラスは、この値を引数の1つのデフォルト値として使用します。

解説 (6)

matplotlib figure size'`のGoogleでの最初のリンクは、[AdjustingImageSize][1] (ページのGoogleキャッシュ)です。

上記ページのテストスクリプトです。同じ画像の異なるサイズの 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.]

2つのメモ。

1.モジュールのコメントと実際の出力は異なります。

2.この回答では、3つの画像を1つの画像ファイルにまとめて、サイズの違いを簡単に確認することができます。

[1]: http://www.scipy.org/Cookbook/Matplotlib/AdjustingImageSize

解説 (1)