Home > Net >  Why does matplotlib arrow not align in the right direction?
Why does matplotlib arrow not align in the right direction?

Time:10-27

My arrows aren't pointing in the right direciton. I don't understand why the normal or the tangent is wrong. Here is the code. This arrow is supposed to be parallel with the line.

S1 = np.array([[-0.4,0.4],
               [-0.6,0.5]])
    
y1 = 0.5
y2 = 0.4
x1 = -0.6
x2 = -0.4
n1 = -(y2-y1)
n2 =  (x2-x1)
x, y = S1.T
plt.plot(x,y)
plt.quiver(-0.5, 0.45, (x2-x1), (y2-y1))
plt.show()

enter image description here

CodePudding user response:

You are not getting the expected result because x- and y-data coordinates are differentially scaled while getting displayed. Please see enter image description here

The right subplot has equal aspect ratio i.e. the difference of 0.1 on Y-axis has the same display length in the subplot as that of X-axis.

Code to reproduce the figure:

import numpy as np
import matplotlib.pyplot as plt

S1 = np.array([[-0.4,0.4],
            [-0.6,0.5]])

y1 = 0.5
y2 = 0.4
x1 = -0.6
x2 = -0.4
n1 = (y2-y1)
n2 =  (x2-x1)
x, y = S1.T
fig, ax = plt.subplots(nrows=1, ncols=2)

ax[0].plot(x, y)
ax[0].quiver(-0.5, 0.45, (x2-x1), (y2-y1))

ax[1].plot(x, y)
ax[1].quiver(-0.5, 0.45, (x2-x1), (y2-y1))
ax[1].set_aspect("equal")

plt.show()
  • Related