我无法使用 python pyplot 将箭头放置在 2D 矢量上并保持笛卡尔网格的其余部分不变

问题描述 投票:0回答:1

我正在使用 matplotlib pyplot 在笛卡尔网格中绘制 2D 向量,我想在它们上有箭头。这是我到目前为止所实现的情节:

Three Vectors

我希望网格线每隔 1 个单位间隔一次,并且我希望网格是“方形”,这意味着 x 轴和 y 轴应该从 -a 到 +a,其中“a”将取决于最长的向量。我已经做到了。我尝试了 pyplot.quiver (plt.quiver) 但随后刚刚提到的其他功能都消失了。她是我的代码:

import numpy as np
import matplotlib.pyplot as plt
M = np.array([[1,5],[-2,2],[4,-5]])
fig, ax = plt.subplots()
rows,cols = M.T.shape
fig = plt.figure(figsize = (10, 10))


maxes = np.amax(abs(M), axis = 0)
lim = np.amax(abs(maxes), axis = 0) +1


for i,l in enumerate(range(0,cols)):
    xs = [0,M[i,0]]
    ys = [0,M[i,1]]
    plt.plot(xs,ys)

list = []                            
for i in range(-lim,lim+1):
    list.append(i)
   
plt.xticks(list) 
plt.yticks(list)

plt.plot(0,0,'ok')                      
plt.axhline(0,linewidth = 1, color="black")
plt.axvline(0,linewidth = 1, color="black")

plt.xlim([-lim,lim])          
plt.ylim([-lim,lim])          
plt.legend(['V'+str(i+1) for i in range(cols)])
plt.grid(which='both')
plt.show()

提前致谢。

python vector 2d matplotlibpyplot
1个回答
0
投票

我找到了我需要的东西。

'''

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.ticker as plticker

fig, ax = plt.subplots()

x=np.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])
y=np.array([-8,-7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7,8])
fig = plt.figure(figsize=[10,10], dpi = 120)
plt.xticks(fontsize=16)
plt.yticks(fontsize=16)
plt.plot(x,y,linewidth = 1, alpha=0.0)

plt.arrow(0, 0, 2, 5, color="blue", linewidth = 2, head_width = 0.3,    length_includes_head = True)
plt.arrow(2, 5, -3, 3, color="blue", linewidth = 2, head_width = 0.3, length_includes_head = True)
plt.arrow(0, 0, -1, 8, color="red", linewidth = 2, head_width = 0.3, length_includes_head = True)

plt.grid(True,"both","both", color="black",linewidth = 2)
plt.show()

'''

我必须绘制一组 (x,y) 有序对并使绘图完全透明 (alpha = 0.0),以获得正确的网格纵横比。我认为可能有一种更Pythonic的方法来做到这一点,但这对我有用。关键是 pyplot.arrow。

Desired Result

© www.soinside.com 2019 - 2024. All rights reserved.