*不*使用鼠标缩放内联 3D matplotlib 图?
Zoom an inline 3D matplotlib figure *without* using the mouse?
This question 解释了如何通过指定仰角和方位角在 matplotlib 中更改 3D 图的 "camera position"。 ax.view_init(elev=10,azim=20)
,例如。
是否有类似的方法来指定数字的缩放——即不使用鼠标?
我能找到的唯一相关问题是 this one,但公认的答案涉及安装另一个库,然后还需要使用鼠标进行缩放。
编辑:
需要说明的是,我不是在谈论更改图形大小(使用 fig.set_size_inches()
或类似方法)。图形尺寸很好;问题是绘制的东西只占了图中的一小部分:
最接近view_init
的解决方案是直接设置ax.dist
。根据 get_proj
"dist is the distance of the eye viewing point from the object point". The initial value is currently hardcoded 和 dist = 10
的文档。较低的值(高于 0!)将导致绘图放大。
注意:此行为并未真正记录在案,可能会更改。在大多数情况下,更改轴的限制以仅绘制相关部分可能是更好的解决方案。您可以使用 ax.autoscale(tight=True)
方便地执行此操作。
工作 IPython/Jupyter 示例:
%matplotlib inline
from IPython.display import display
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)
# Plot a basic wireframe.
ax.view_init(90, 0)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.close()
from ipywidgets import interact
@interact(dist=(1, 20, 1))
def update(dist=10):
ax.dist = dist
display(fig)
输出
dist = 10
dist = 5
This question 解释了如何通过指定仰角和方位角在 matplotlib 中更改 3D 图的 "camera position"。 ax.view_init(elev=10,azim=20)
,例如。
是否有类似的方法来指定数字的缩放——即不使用鼠标?
我能找到的唯一相关问题是 this one,但公认的答案涉及安装另一个库,然后还需要使用鼠标进行缩放。
编辑:
需要说明的是,我不是在谈论更改图形大小(使用 fig.set_size_inches()
或类似方法)。图形尺寸很好;问题是绘制的东西只占了图中的一小部分:
最接近view_init
的解决方案是直接设置ax.dist
。根据 get_proj
"dist is the distance of the eye viewing point from the object point". The initial value is currently hardcoded 和 dist = 10
的文档。较低的值(高于 0!)将导致绘图放大。
注意:此行为并未真正记录在案,可能会更改。在大多数情况下,更改轴的限制以仅绘制相关部分可能是更好的解决方案。您可以使用 ax.autoscale(tight=True)
方便地执行此操作。
工作 IPython/Jupyter 示例:
%matplotlib inline
from IPython.display import display
from mpl_toolkits.mplot3d import axes3d
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Grab some test data.
X, Y, Z = axes3d.get_test_data(0.05)
# Plot a basic wireframe.
ax.view_init(90, 0)
ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10)
plt.close()
from ipywidgets import interact
@interact(dist=(1, 20, 1))
def update(dist=10):
ax.dist = dist
display(fig)
输出
dist = 10
dist = 5