How to plot vectors in python using matplotlib

后端 未结 6 686
太阳男子
太阳男子 2020-12-24 03:30

I am taking a course on linear algebra and I want to visualize the vectors in action, such as vector addition, normal vector, so on.

For instance:

         


        
6条回答
  •  轮回少年
    2020-12-24 03:47

    What did you expect the following to do?

    v1 = [0,0],[M[i,0],M[i,1]]
    v1 = [M[i,0]],[M[i,1]]
    

    This is making two different tuples, and you overwrite what you did the first time... Anyway, matplotlib does not understand what a "vector" is in the sense you are using. You have to be explicit, and plot "arrows":

    In [5]: ax = plt.axes()
    
    In [6]: ax.arrow(0, 0, *v1, head_width=0.05, head_length=0.1)
    Out[6]: 
    
    In [7]: ax.arrow(0, 0, *v2, head_width=0.05, head_length=0.1)
    Out[7]: 
    
    In [8]: plt.ylim(-5,5)
    Out[8]: (-5, 5)
    
    In [9]: plt.xlim(-5,5)
    Out[9]: (-5, 5)
    
    In [10]: plt.show()
    

    Result:

提交回复
热议问题