I want to build perpendicular vectors in Python

I would just like to plot perpendicular vectors in 2D. I implemented two ways to build them in the code below, but the vectors do not โ€œlookโ€ perpendicular to me when the graphs are drawn. If that matters, I use Spyder.

import numpy as np import matplotlib.pyplot as plt x1=[0,0,4,3] x2=[0,0,-3,4] x3=[0,0,3,-4] soa =np.array([x1,x2,x3]) X,Y,U,V = zip(*soa) plt.figure() ax = plt.gca() ax.quiver(X,Y,U,V,angles='xy',scale_units='xy',scale=1) ax.set_xlim([-10,10]) ax.set_ylim([-10,10]) plt.draw() plt.show() import pylab as pl from matplotlib import collections as mc lines = [[(0, 1), (4, 3)], [(-3, 4), (3, -4)]] c = np.array([(1, 0, 0, 1), (0, 1, 0, 1), (0, 0, 1, 1)]) lc = mc.LineCollection(lines, colors=c, linewidths=2) fig, ax = pl.subplots() ax.add_collection(lc) ax.autoscale() ax.margins(0.1) 
+5
source share
2 answers

Your problem is that the size of the device is different from the x and y axes. You must make them equal.

In matplotlib.pyplot add the line

 plt.axes().set_aspect('equal') 

before plotting with

 plt.show() 

I get this result in the IPython console in Spyder:

enter image description here

In pylab add the line

 ax.set_aspect('equal') 

in the end. However, these line segments still do not look perpendicular, and this is because they really are not perpendicular. The slope of your first segment of the red line is 2/3, so your second segment of the green line should have a slope of -3/2, but in fact it has a slope of -4/3. Maybe change your line to

 lines = [[(0, 1), (4, 3)], [(-3, 4), (3, -5)]] 

(I changed the ending -4 to -5) to get the correct second slope. You get a change from this first digit to the second:

enter image description here

enter image description here

and the latter looks perpendicular.

+3
source

The problem is the aspect ratio of the picture.

Using:

 plt.figure(figsize=(6,6)) 
+1
source

Source: https://habr.com/ru/post/1261432/


All Articles