2016-12-17 11 views
5

我想簡單地在2D中繪製垂直向量。我已經實現了兩種在下面的代碼中繪製它們的方法,但繪製圖時,矢量不會「看起來」垂直於我。如果它有什麼不同,我使用Spyder。我想在Python中繪製垂直向量

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) 

回答

3

您的問題是,單位的大小在x和y軸上有所不同。你需要強制他們平等。

在matplotlib.pyplot,添加行

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

您演示

plt.show() 

圖就在我得到這樣的結果在IPython的控制檯中的Spyder:

enter image description here

在pylab中,添加行

ax.set_aspect('equal') 

最後。但是,這些線段仍然看起來不垂直,這是因爲它們確實不是垂直的。你的第一條紅色線段的斜率爲2/3,所以你的第二條綠色線段的斜率應爲-3/2,但實際上斜率爲-4/3。也許改變你的線

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

(我改變了結束-4爲-5),以獲得正確的第二斜率。你從這個第一個數字的變化,以第二:

enter image description here

enter image description here

與去年看起來垂直。

1

問題是圖形畫布的高寬比。

用途:

plt.figure(figsize=(6,6))