How to plot vectors in python using matplotlib

后端 未结 6 702
太阳男子
太阳男子 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:37

    How about something like

    import numpy as np
    import matplotlib.pyplot as plt
    
    V = np.array([[1,1], [-2,2], [4,-7]])
    origin = np.array([[0, 0, 0],[0, 0, 0]]) # origin point
    
    plt.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], scale=21)
    plt.show()
    

    Then to add up any two vectors and plot them to the same figure, do so before you call plt.show(). Something like:

    plt.quiver(*origin, V[:,0], V[:,1], color=['r','b','g'], scale=21)
    v12 = V[0] + V[1] # adding up the 1st (red) and 2nd (blue) vectors
    plt.quiver(*origin, v12[0], v12[1])
    plt.show()
    

    NOTE: in Python2 use origin[0], origin[1] instead of *origin

提交回复
热议问题