3D Matplotlib 中的动态点操作 [英] Dynamic Point Manipulation in 3D Matplotlib

查看:36
本文介绍了3D Matplotlib 中的动态点操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试在matplotlib中操作图形时,我遇到了一些有趣的事情.使用 plot 函数时,可以通过别名修改 x 和 y 坐标,但是 z 不能.这容易解释吗?我已经包含了一个小例子,请注意,我试图让点沿着圆柱体在螺旋中移动.

In trying to manipulate a graph in matplotlib, I came across something interesting. when using the plot function, x and y coordinates can be modified through aliasing, however the z cannot. Is this easily explained? I have included a small example, note that I was trying to get the point to move in a helix along the cylinder.

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.set_xlabel("X")
ax.set_ylabel("Y")
ax.set_zlabel("Z")

radius = 8

# Cylinder
x = np.linspace(-radius, radius, 100)
z = np.linspace(0, 2, 100)
X, Z = np.meshgrid(x, z)

Y = np.sqrt(radius**2 - X**2)

ax.plot_surface(X, Y, Z, alpha=0.3, linewidth=0)
ax.plot_surface(X, (-Y), Z,alpha=0.3, linewidth=0)

X = np.array(np.zeros(1))
Y = np.array(np.zeros(1))
Z = np.array(np.zeros(1))

X[0]= radius*np.cos(np.deg2rad(0))
Y[0]= radius*np.sin(np.deg2rad(0))
Z[0]= 0.

ax.plot(X,Y,Z, 'or',markersize=3)

for i in range(0,360,5):

    X[0]= radius*np.cos(np.deg2rad(i))  
    Y[0]= radius*np.sin(np.deg2rad(i))
    Z[0]= i/140.

    plt.draw()
    plt.pause(0.01)

作为参考,此练习的最终目标是能够基于传感器的输入在三个维度上实时定位点.

For reference, the end goal of this exercise is to be able to position the point based on sensor inputs in real-time, in three dimensions.

感谢所有对这种情况为什么发生以及可能的解决方案有深刻见解的人!

Thank you to anyone who has insight as to why this is occurring and possible solutions!

推荐答案

这确实很有趣. 或 set_xdataz 方向也不可用.查看源代码(art3d.py),似乎完全忽略了 z 值:

This is indeed interesting.. Line3D is derived from Line2D, so functions like get_xdata or set_xdata are also not available in the z direction. Looking into the source code (art3d.py), it seems that the z values are completely ignored:

def draw(self, renderer):
    xs3d, ys3d, zs3d = self._verts3d
    xs, ys, zs = proj3d.proj_transform(xs3d, ys3d, zs3d, renderer.M)
    self.set_data(xs, ys)
    lines.Line2D.draw(self, renderer)

但也许我没有正确理解 Line3D 的工作原理...

But perhaps I don't correctly understand how the Line3D's work...

但是有一个函数可以操作 z 值( set_3d_properties ),但是由于该函数调用 get_xdata get_ydata 本身,我可以让它工作的唯一方法是:

There is however a function available to manipulate the z values (set_3d_properties), but since that function calls get_xdata and get_ydata itself, the only way I could get this to work is:

p = ax.plot(X,Y,Z, 'or', markersize=3)[0]

for i in range(0,360,5):
    p.set_xdata(radius*np.cos(np.deg2rad(i)))
    p.set_ydata(radius*np.sin(np.deg2rad(i)))
    p.set_3d_properties(zs=i/140.)

这篇关于3D Matplotlib 中的动态点操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆