如何使用python/matplotlib为3D绘图设置“相机位置”?

rryofs0p  于 2023-05-01  发布在  Python
关注(0)|答案(5)|浏览(282)

我正在学习如何使用mplot3d来生成漂亮的3d数据图,到目前为止我很高兴。我现在要做的是一个旋转表面的动画。为此,我需要为3D投影设置相机位置。我想这是可能的,因为在交互式使用matplotlib时,可以使用鼠标旋转曲面。但是我怎么能从一个脚本中做到这一点呢?我在mpl_toolkits中发现了很多转换。mplot3d.proj3d,但我找不到如何使用这些为我的目的,我没有找到任何例子,我试图做什么。

7cwmlq89

7cwmlq891#

通过“相机位置”,听起来像是要调整用于查看3D打印的仰角和方位角。您可以使用ax.view_init来设置。我使用下面的脚本首先创建了图,然后我确定了一个好的高程,或elev,从那里查看我的图。然后,我调整了方位角,或azim,以改变我的图周围的整个360度,保存每个示例的图(并注意我保存图时的方位角)。对于更复杂的摄影机平移,可以调整仰角和Angular 以获得所需的效果。

from mpl_toolkits.mplot3d import Axes3D
    ax = Axes3D(fig)
    ax.scatter(xx,yy,zz, marker='o', s=20, c="goldenrod", alpha=0.6)
    for ii in xrange(0,360,1):
        ax.view_init(elev=10., azim=ii)
        savefig("movie%d.png" % ii)
eblbsuwk

eblbsuwk2#

将“摄影机”位置应用于新的绘图会很方便。所以我画了图,然后用鼠标改变距离移动图。然后尝试在另一个图上复制包括距离的视图。我找到了那个Axx。get_axes()获取一个对象。azim和elev.
在Python…

axx=ax1.get_axes()
azm=axx.azim
ele=axx.elev
dst=axx.dist       # ALWAYS GIVES 10
#dst=ax1.axes.dist # ALWAYS GIVES 10
#dst=ax1.dist      # ALWAYS GIVES 10

3D图形..

ax2.view_init(elev=ele, azim=azm) #Works!
ax2.dist=dst                       # works but always 10 from axx

编辑1...好吧,相机的位置是错误的思考方式。dist值。它作为整个图的一种hackey标量乘数,位于所有内容之上。
这适用于视图的放大/缩放:

xlm=ax1.get_xlim3d() #These are two tupples
ylm=ax1.get_ylim3d() #we use them in the next
zlm=ax1.get_zlim3d() #graph to reproduce the magnification from mousing
axx=ax1.get_axes()
azm=axx.azim
ele=axx.elev

后来的Graph。..

ax2.view_init(elev=ele, azim=azm) #Reproduce view
ax2.set_xlim3d(xlm[0],xlm[1])     #Reproduce magnification
ax2.set_ylim3d(ylm[0],ylm[1])     #...
ax2.set_zlim3d(zlm[0],zlm[1])     #...
v1l68za4

v1l68za43#

改变azimdistelev的最小示例

添加一些简单的示例图像到解释在:https://stackoverflow.com/a/12905458/895245
下面是我的测试程序:

#!/usr/bin/env python3

import sys

import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

fig = plt.figure()
ax = fig.gca(projection='3d')

if len(sys.argv) > 1:
    azim = int(sys.argv[1])
else:
    azim = None
if len(sys.argv) > 2:
    dist = int(sys.argv[2])
else:
    dist = None
if len(sys.argv) > 3:
    elev = int(sys.argv[3])
else:
    elev = None

# Make data.
X = np.arange(-5, 6, 1)
Y = np.arange(-5, 6, 1)
X, Y = np.meshgrid(X, Y)
Z = X**2

# Plot the surface.
surf = ax.plot_surface(X, Y, Z, linewidth=0, antialiased=False)

# Labels.
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')

if azim is not None:
    ax.azim = azim
if dist is not None:
    ax.dist = dist
if elev is not None:
    ax.elev = elev

print('ax.azim = {}'.format(ax.azim))
print('ax.dist = {}'.format(ax.dist))
print('ax.elev = {}'.format(ax.elev))

plt.savefig(
    'main_{}_{}_{}.png'.format(ax.azim, ax.dist, ax.elev),
    format='png',
    bbox_inches='tight'
)

不带参数运行它会给出默认值:

ax.azim = -60
ax.dist = 10
ax.elev = 30

main_-60_10_30.png

变化azim

方位角是绕z轴e的旋转。例如:

  • 0表示“从+x方向看”
  • 90表示“从+y方向看”

main_-60_10_30.png

main_0_10_30.png

main_60_10_30.png

变化dist

dist似乎是数据坐标中距中心可见点的距离。
main_-60_10_30.png

main_-60_5_30.png

main_-60_20_-30.png

变化elev

由此可知,elev是眼睛与xy平面之间的夹角。
main_-60_10_60.png

main_-60_10_30.png

main_-60_10_0.png

main_-60_10_-30.png

在matpotlib上测试==3.2.2.

byqmnocz

byqmnocz4#

请尝试以下代码以查找最佳摄像机位置

使用if子句中提到的键盘键移动绘图的视角

Use **print** to get the camera positions

def move_view(event):
    ax.autoscale(enable=False, axis='both') 
    koef = 8
    zkoef = (ax.get_zbound()[0] - ax.get_zbound()[1]) / koef
    xkoef = (ax.get_xbound()[0] - ax.get_xbound()[1]) / koef
    ykoef = (ax.get_ybound()[0] - ax.get_ybound()[1]) / koef
    ## Map an motion to keyboard shortcuts
    if event.key == "ctrl+down":
        ax.set_ybound(ax.get_ybound()[0] + xkoef, ax.get_ybound()[1] + xkoef)
    if event.key == "ctrl+up":
        ax.set_ybound(ax.get_ybound()[0] - xkoef, ax.get_ybound()[1] - xkoef)
    if event.key == "ctrl+right":
        ax.set_xbound(ax.get_xbound()[0] + ykoef, ax.get_xbound()[1] + ykoef)
    if event.key == "ctrl+left":
        ax.set_xbound(ax.get_xbound()[0] - ykoef, ax.get_xbound()[1] - ykoef)
    if event.key == "down":
        ax.set_zbound(ax.get_zbound()[0] - zkoef, ax.get_zbound()[1] - zkoef)
    if event.key == "up":
        ax.set_zbound(ax.get_zbound()[0] + zkoef, ax.get_zbound()[1] + zkoef)
    # zoom option
    if event.key == "alt+up":
        ax.set_xbound(ax.get_xbound()[0]*0.90, ax.get_xbound()[1]*0.90)
        ax.set_ybound(ax.get_ybound()[0]*0.90, ax.get_ybound()[1]*0.90)
        ax.set_zbound(ax.get_zbound()[0]*0.90, ax.get_zbound()[1]*0.90)
    if event.key == "alt+down":
        ax.set_xbound(ax.get_xbound()[0]*1.10, ax.get_xbound()[1]*1.10)
        ax.set_ybound(ax.get_ybound()[0]*1.10, ax.get_ybound()[1]*1.10)
        ax.set_zbound(ax.get_zbound()[0]*1.10, ax.get_zbound()[1]*1.10)
    
    # Rotational movement
    elev=ax.elev
    azim=ax.azim
    if event.key == "shift+up":
        elev+=10
    if event.key == "shift+down":
        elev-=10
    if event.key == "shift+right":
        azim+=10
    if event.key == "shift+left":
        azim-=10

    ax.view_init(elev= elev, azim = azim)

    # print which ever variable you want 

    ax.figure.canvas.draw()

fig.canvas.mpl_connect("key_press_event", move_view)

plt.show()
w1jd8yoj

w1jd8yoj5#

Q:如何在matplotlib中设置view?

对于3D绘图,如何固定视图?

A:通过设置属性ax.azimax.level

ax.elev = 0
ax.azim = 270  # xz view

ax.elev = 0
ax.azim = 0    # yz view

ax.elev = 0
ax.azim = -90  # xy view

相关问题