如何裁剪具有纵横比的Axes3D图? [英] How do I crop an Axes3D plot with square aspect ratio?
问题描述
这是一个准系统示例:
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()
这就是我得到的:
这就是我想要的:
换句话说,我希望绘图本身具有正方形的纵横比,而不是全部像这样伸展:
感谢 https://stackoverflow.com/a/31364297/125507 >
但是我希望将这些图查看的窗口是矩形的,顶部和底部被裁剪掉(因为顶部和底部只有空白,并且我正在生成多个动画GIF,所以我不能轻易发布-将其加工成我想要的形状).
基本上,我正在制作此动画,并且我想要相同的东西,但没有所有空格:
为了跟进我的评论并通过设置较小的r
并使用subplots_adjust
删除子图边距来显示示例:
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
不确定操作者正在寻找的是这个...
Here's a barebones example:
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()
This is what I get:
This is what I want:
In other words, I want the plots themselves to have a square aspect ratio, not all stretched out like this:
and I got that part working thanks to https://stackoverflow.com/a/31364297/125507
but I want the windows looking into those plots to be rectangular, with the top and bottom cropped out (since there will just be white space on top and bottom and I'm generating multiple animated GIFs so I can't easily post-process it to the shape I want).
Basically I am making this animation, and I want the same thing but without all the whitespace:
In order to follow up on my comments and show examples by settting a smaller r
and removing subplot margins using subplots_adjust
:
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()
This will give tighter subplots, as the defaults for SubplotParams are as follows:
left : 0.125
right : 0.9
bottom : 0.1
top : 0.9
wspace : 0.2
hspace : 0.2
not sure it is this the OP is looking for though...
这篇关于如何裁剪具有纵横比的Axes3D图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!