2015-08-08 55 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