2016-07-01 10 views
3

hereというコードを使用して、以下のようなプロットを作成します。3Dプロットアスペクト比[matplotlib]

問題は、縦横比を調整して、つまりz軸に沿って伸ばして、すべての積み重なった画像が多かれ少なかれ見えるようにすることです。それを行う簡単な方法はありますか?

enter image description here

+1

関連:http://stackoverflow.com/a/10328142/1813349、第二のアプローチ。 –

+0

私のOS Xでは、(たとえ 'sudo'アクセスでも)python binディレクトリのファイルを変更できません。これはいくつかの奇妙な保護のためです([this](http://stackoverflow.com/questions/32659348/operation-not-permitted-when-on-root-el-capitan-rootless-disabled)を参照))。そして、この時点で私はあなたの他の解決策が働くようにこの仕事をするのは面倒です:)しかし、おそらく将来的にはそれを行うことは良い考えです。 –

答えて

1

をしたいが見えるが、我々は問題を回避猿パッチング我々の方法を試すことができます。ここで私は何ができる最高のその場しのぎです:

from mpl_toolkits.mplot3d import proj3d 

def make_get_proj(self, rx, ry, rz): 
    ''' 
    Return a variation on :func:`~mpl_toolkit.mplot2d.axes3d.Axes3D.getproj` that 
    makes the box aspect ratio equal to *rx:ry:rz*, using an axes object *self*. 
    ''' 

    rm = max(rx, ry, rz) 
    kx = rm/rx; ky = rm/ry; kz = rm/rz; 

    # Copied directly from mpl_toolkit/mplot3d/axes3d.py. New or modified lines are 
    # marked by ## 
    def get_proj(): 
     relev, razim = np.pi * self.elev/180, np.pi * self.azim/180 

     xmin, xmax = self.get_xlim3d() 
     ymin, ymax = self.get_ylim3d() 
     zmin, zmax = self.get_zlim3d() 

     # transform to uniform world coordinates 0-1.0,0-1.0,0-1.0 
     worldM = proj3d.world_transformation(xmin, xmax, 
              ymin, ymax, 
              zmin, zmax) 

     # adjust the aspect ratio       ## 
     aspectM = proj3d.world_transformation(-kx + 1, kx, ## 
               -ky + 1, ky, ## 
               -kz + 1, kz) ## 

     # look into the middle of the new coordinates 
     R = np.array([0.5, 0.5, 0.5]) 

     xp = R[0] + np.cos(razim) * np.cos(relev) * self.dist 
     yp = R[1] + np.sin(razim) * np.cos(relev) * self.dist 
     zp = R[2] + np.sin(relev) * self.dist 
     E = np.array((xp, yp, zp)) 

     self.eye = E 
     self.vvec = R - E 
     self.vvec = self.vvec/proj3d.mod(self.vvec) 

     if abs(relev) > np.pi/2: 
      # upside down 
      V = np.array((0, 0, -1)) 
     else: 
      V = np.array((0, 0, 1)) 
     zfront, zback = -self.dist, self.dist 

     viewM = proj3d.view_transformation(E, R, V) 
     perspM = proj3d.persp_transformation(zfront, zback) 
     M0 = np.dot(viewM, np.dot(aspectM, worldM)) ## 
     M = np.dot(perspM, M0) 
     return M 
    return get_proj 

# and later in the code: 
ax.get_proj = make_get_proj(ax, 1, 1, 2) 
ax.set_aspect(1.0) 

3D plot inside a 1:1:2 box rendered by Matplotlib

+0

それはうまくいきました!ありがとうございます。しかし、それはちょっと変です。 –

0

あなたが垂直方向にキャンバスを伸ばしてみましたがありますか?たとえば、

plt.figure(figsize=(5,10)) 

などとすると、より多くのスペースが必要です。しかし、それがあなたの問題を解決するかどうかはわかりません。

+1

これはアスペクト比には影響しません。バウンディングボックスのアスペクトに影響を与えるには、 'ax.set_aspect()'のようなものがあるはずです。 –

0

たぶん、あなたはこの意志はあなたの姿は、Z軸に沿ってより多くのスペースを持っています

plt.figure.subplots_adjust(bottom = 0.05) 

を試すことができ、私はあなたが欲しいものだと思います。それを行うに「正しい」方法がないよう

それとも、

ax.set_xlim(xmin,xmax) 
+0

うまくいきませんでした:( –

+0

'ax.set_zlim'は単に軸を絞るだけですが、境界ボックスのアスペクト比には影響しません。 –

+0

subplots_adjustは機能しません。本当に欲しいものは分かりません。 – disccip

関連する問題