绘制平面和正交向量时结果错误 [英] Wrong result when plotting a plane and an orthogonal vector

查看:28
本文介绍了绘制平面和正交向量时结果错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以 3D 形式计算一个向量相对于另一个向量的分量.显示结果时,虽然我对它下面的简单数学很有信心,但可视化显然是错误的.

I need to compute the components of a vector respect to another vector, in 3D. When displaying the results, while I am confident about the simple math under it, the visualization is plain wrong.

我写了一个小脚本来重现这个问题.该平面是 z = x + y,即 x + y - z = 0.那么与其正交的向量是 (1, 1, -1).然而,当用quiver绘制它时,视觉效果是错误的.

I wrote a little script to reproduce the problem. The plane is z = x + y, which is x + y - z = 0. A vector orthogonal to it is then (1, 1, -1). However, when plotting it with quiver, the visual result is wrong.

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')

X,Y = np.meshgrid(np.arange( -1,  1, 0.1), np.arange( -1, 1, 0.1))
XX = X.flatten()
YY = Y.flatten()
Z = X + Y 
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.2)

ax.scatter(1, 1, -1, c="orange", s=20, marker='o')

ax.quiver(0, 0, 0, 1, 1, -1, color="blue")
plt.show()

quiver 探测实际绘制指向目标点 (1, 1, -1) 的向量,平面实际上是正确的,但它们不是正交的.

quiver probes to actually draw the vector pointing to the target point (1, 1, -1) and the plane is actually the correct one, but they are not orthogonal.

我是否遗漏了一些非常明显的东西,还是仅仅是视角问题?

Am I missing something extremely obvious or is it simply a problem of perspective?

推荐答案

我认为这是一个扩展问题.您可以使用 ax.set_xlim3d 为所有轴设置相同的范围.

I think it's a scaling issue. You can use ax.set_xlim3d to set the same range for all axis.

看起来不错:

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')

X,Y = np.meshgrid(np.arange( -1,  1, 0.1), np.arange( -1, 1, 0.1))
XX = X.flatten()
YY = Y.flatten()
Z = X + Y 
ax.plot_surface(X, Y, Z, rstride=1, cstride=1, alpha=0.2)

ax.scatter(1, 1, -1, c="orange", s=20, marker='o')

ax.quiver(0, 0, 0, 1, 1, -1, color="blue")

ax.set_xlim3d(-1,1) 
ax.set_ylim3d(-1,1) 
ax.set_zlim3d(-1,1) 

plt.show()

这篇关于绘制平面和正交向量时结果错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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