3回目の魅力。私の推測では、これはバグであり、Zhenya's answerはそれが最新バージョンで修正されていることを示唆しています。私は、バージョン0.99.1.1を持っていると私は、次の解決策を作成しました:
以下は私の失敗し、まだうまくいけば、有益な試みをしている:これは「force.png」の
import matplotlib.pyplot as plt
import numpy as np
def forceAspect(ax,aspect=1):
im = ax.get_images()
extent = im[0].get_extent()
ax.set_aspect(abs((extent[1]-extent[0])/(extent[3]-extent[2]))/aspect)
data = np.random.rand(10,20)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data)
ax.set_xlabel('xlabel')
ax.set_aspect(2)
fig.savefig('equal.png')
ax.set_aspect('auto')
fig.savefig('auto.png')
forceAspect(ax,aspect=1)
fig.savefig('force.png')
です。
セカンド回答:それはaxes.set_aspect()
に似た何かをするよう
私のオリジナルの答えは "以下、過剰です。私はあなたがaxes.set_aspect('auto')
を使用したいと思う。このような場合は、なぜ私は理解していないが、それは私のための正方形の画像のプロットを作成し、例えば、このスクリプト: と1と:
import matplotlib.pyplot as plt
import numpy as np
data = np.random.rand(10,20)
fig = plt.figure()
ax = fig.add_subplot(111)
ax.imshow(data)
ax.set_aspect('equal')
fig.savefig('equal.png')
ax.set_aspect('auto')
fig.savefig('auto.png')
は「等しい」アスペクト比の画像プロットを作成します'auto'縦横比:
「元の回答」の下に示されているコードは、明示的に制御された縦横比の開始点を示していますが、imshowが呼び出されると無視されるようです。
オリジナル回答:
は、ここでは、所望のアスペクト比を得るように、サブプロットのパラメータを調整しますルーチンの例です:
import matplotlib.pyplot as plt
def adjustFigAspect(fig,aspect=1):
'''
Adjust the subplot parameters so that the figure has the correct
aspect ratio.
'''
xsize,ysize = fig.get_size_inches()
minsize = min(xsize,ysize)
xlim = .4*minsize/xsize
ylim = .4*minsize/ysize
if aspect < 1:
xlim *= aspect
else:
ylim /= aspect
fig.subplots_adjust(left=.5-xlim,
right=.5+xlim,
bottom=.5-ylim,
top=.5+ylim)
fig = plt.figure()
adjustFigAspect(fig,aspect=.5)
ax = fig.add_subplot(111)
ax.plot(range(10),range(10))
fig.savefig('axAspect.png')
これがそうのような図を生成します。
数字の中に複数のサブプロットがある場合は、 ouldは、提供されたルーチンに、yとxのサブプロットの数をキーワードパラメータ(それぞれ1にデフォルト設定)として含める必要があります。これらの数値とhspace
とwspace
のキーワードを使用すると、すべてのサブプロットを正しいアスペクト比にすることができます。
偶然、 'ax.axis( 'equal')'を試しましたか?誰もが言ったように、あなたがしたことはうまくいくはずですが、 'ax.axis'は回避策を試みる別のルートかもしれません。 –
http://matplotlib.org/examples/pylab_examples/equal_aspect_ratio.html –