2015-08-08 117 views
14

這裏有一個準系統例如:如何裁剪一個具有方形寬高比的Axes3D圖?

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 

fig = plt.figure() 
f = fig.add_subplot(2, 1, 1, projection='3d') 
t = fig.add_subplot(2, 1, 2, projection='3d') 

# axes 
for d in {f, t}: 
    d.plot([-1, 1], [0, 0], [0, 0], color='k', alpha=0.8, lw=2) 
    d.plot([0, 0], [-1, 1], [0, 0], color='k', alpha=0.8, lw=2) 
    d.plot([0, 0], [0, 0], [-1, 1], color='k', alpha=0.8, lw=2) 

f.dist = t.dist = 5.2 # 10 is default 

plt.tight_layout() 
f.set_aspect('equal') 
t.set_aspect('equal') 

r = 6 
f.set_xlim3d([-r, r]) 
f.set_ylim3d([-r, r]) 
f.set_zlim3d([-r, r]) 

t.set_xlim3d([-r, r]) 
t.set_ylim3d([-r, r]) 
t.set_zlim3d([-r, r]) 

f.set_axis_off() 
t.set_axis_off() 

plt.draw() 
plt.show() 

這就是我得到:

square viewports

這就是我想要的:

rectangular viewports

換句話說,我想這些情節本身具有正方形的長寬比,並非全部如此展開:

stretched out

,我得到的那部分工作由於https://stackoverflow.com/a/31364297/125507

但我希望尋找到那些地塊的窗戶是長方形,頂部和底部裁剪出來(因爲也只是空白在頂部和底部,我正在生成多個動畫GIF,所以我不能輕鬆將其後處理到我想要的形狀)。

基本上我製作這個動畫,我希望同樣的事情,但沒有所有的空白:

animation

+0

我不是你的usre例如足夠的代表,你嘗試過' fig.subplots_adjust(top = 1,bottom = 0,right = 1,left = 0, hspace = 0,wspace = 0)'? – rll

+0

除此之外,你需要收緊你的'r' – rll

+0

@rll是的,我縮短了一個軸,從我實際想要的,以說明他們都是相同的長度 – endolith

回答

3

爲了通過settting一個跟進我的意見,並顯示實例使用subplots_adjustr和刪除插曲邊距:


import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 

fig = plt.figure() 
f = fig.add_subplot(2, 1, 1, projection='3d') 
t = fig.add_subplot(2, 1, 2, projection='3d') 

# axes 
for d in {f, t}: 
    d.plot([-1, 1], [0, 0], [0, 0], color='k', alpha=0.8, lw=2) 
    d.plot([0, 0], [-1, 1], [0, 0], color='k', alpha=0.8, lw=2) 
    d.plot([0, 0], [0, 0], [-1, 1], color='k', alpha=0.8, lw=2) 

f.dist = t.dist = 5.2 # 10 is default 

plt.tight_layout() 
f.set_aspect('equal') 
t.set_aspect('equal') 

r = 1 
f.set_xlim3d([-r, r]) 
f.set_ylim3d([-r, r]) 
f.set_zlim3d([-r, r]) 

t.set_xlim3d([-r, r]) 
t.set_ylim3d([-r, r]) 
t.set_zlim3d([-r, r]) 

f.set_axis_off() 
t.set_axis_off() 

fig.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, 
      hspace = 0, wspace = 0) 

plt.draw() 
plt.show() 

這會給更緊副區,作爲用於SubplotParams默認值如下:

左:0.125

右:0.9

底部:0.1

頂部:0.9

wspace :0.2

hspace:0.2

不知道它這是OP正在尋找,但...

enter image description here

+0

雖然每個圖仍然是正方形,但是我想讓它們仍然具有等軸的矩形 – endolith

-1

既然你想矩形圖,你不能使用set_aspect('equal'),相反,你必須調整你的地塊的限制說明縱橫比的差異。

在下面的嘗試中,我使用的軸的bbox得到軸的heightwidth和所使用的縱橫比的重新縮放XY軸。(我假設兩個圖都具有相同的尺寸,但是您也可以繪製不同尺寸的圖,您只需爲每個尺寸單獨調整軸限制)。

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 

fig = plt.figure() 
f = fig.add_subplot(2, 1, 1, projection='3d') 
t = fig.add_subplot(2, 1, 2, projection='3d') 

# axes 
for d in {f, t}: 
    d.plot([-1, 1], [0, 0], [0, 0], color='k', alpha=0.8, lw=2) 
    d.plot([0, 0], [-1, 1], [0, 0], color='k', alpha=0.8, lw=2) 
    d.plot([0, 0], [0, 0], [-1, 1], color='k', alpha=0.8, lw=2) 

f.dist = t.dist = 5.2 # 10 is default 

plt.tight_layout() 
# Cannot use 'equal' aspect for a rectangular plot 
# f.set_aspect('equal') 
# t.set_aspect('equal') 

# get the dimensions of the axes from its bbox 
f_bbox = f.get_position() 
f_height = f_bbox.height 
f_width = f_bbox.width 


r = 6 
f.set_xlim3d([-1 * f_width/f_height * r, f_width/f_height * r]) 
f.set_ylim3d([-1 * f_width/f_height * r, f_width/f_height * r]) 
f.set_zlim3d([-r, r]) 

t.set_xlim3d([-1 * f_width/f_height * r, f_width/f_height * r]) 
t.set_ylim3d([-1 * f_width/f_height * r, f_width/f_height * r]) 
t.set_zlim3d([-r, r]) 

f.set_axis_off() 
t.set_axis_off() 

plt.draw() 
plt.show() 

注:在這張圖片中,我添加了一個背景色來突出情節的長方形

+0

但它們在旋轉時不保持縱橫比。視口需要被裁剪成一個矩形,而不是縮小成一個 – endolith