Skip to content Skip to sidebar Skip to footer

Wrong Result When Plotting A Plane And An Orthogonal Vector

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 i

Solution 1:

The orange point is correct, rigth? So the vector pointing to that point is correct as well, because it starts at the origin. So the only problem is that while the vector is correct and orthorgonal to the plane in data space, it is not in display space.

To make display space have an equal aspect ratio, there are pretty hacky solutions but the easiest is to

  1. Make a square figure
  2. Use equal margins on all sides
  3. Use equal limits for all axes.

This could look like this.

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

fig = plt.figure(figsize=(6,6))
ax = fig.add_subplot(111, projection='3d')
fig.subplots_adjust(.1,.1,.9,.9)
ax.set(xlim=(-2,2), ylim=(-2,2), zlim=(-2,2))

X,Y = np.meshgrid(np.arange( -1,  1, 0.1), np.arange( -1, 1, 0.1))
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()

enter image description here


Solution 2:

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

It looks fine like this:

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

Post a Comment for "Wrong Result When Plotting A Plane And An Orthogonal Vector"