更改3D图中的旋转中心
我在matplotlib里做了一个3D线图,使用了下面的代码:
def view(self):
from mpl_toolkits.mplot3d import Axes3D #noqa
import matplotlib.pyplot as plt
ax = plt.figure().gca(projection='3d')
history = np.array(self.position_history)
x, y ,z = history[:, 0], history[:, 1], history[:, 2]
ax.plot(x, y, z)
plt.show()
这里的history
是一个Mx3的点数组。这个图正常显示,我可以点击并拖动来互动地调整视角的方位和高度。我还可以通过右键点击和拖动来放大缩小。
不过我在想,能不能改变放大和移动的中心点?我想要放大到右上角,然后以右上角为中心来移动。如果你用过solidworks或者其他CAD软件,你就会知道我想要的效果。这个能做到吗?如果不能互动地做到,那我能通过编程实现吗?
最后,如果matplotlib里做不到这些,有没有其他库可以实现我想要的功能?
2 个回答
4
不确定这是否能满足你的需求,但你可以在第一次绘图时定义中心。在这里,“c_x”是x轴上的旋转中心,-/+ 200表示这个轴在两个方向上各有200个单位。同样的道理也适用于y轴和z轴。
如果让用户在绘图之前选择中心,这也可以作为一个简单的解决办法。
http://matplotlib.org/api/axes_api.html?highlight=set_xbound#matplotlib.axes.Axes.set_xbound
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.autoscale(enable=False,axis='both') #you will need this line to change the Z-axis
ax.set_xbound(c_x-200, c_x+200)
ax.set_ybound(c_y-200, c_y+200)
ax.set_zbound(c_z-200, c_z+200)