How to plot 2d math vectors with matplotlib?

后端 未结 2 560
梦毁少年i
梦毁少年i 2020-12-13 05:14

How can we plot 2D math vectors with matplotlib? Does anyone have an example or suggestion about that?

I have a couple of vectors stored as 2D nu

相关标签:
2条回答
  • 2020-12-13 05:19

    It's pretty straightforward. Hope this example helps.

    import matplotlib.pyplot as plt
    import numpy as np
    x = np.random.normal(10,5,100)
    y = 3 + .5*x + np.random.normal(0,1,100)
    myvec = np.array([x,y])
    plt.plot(myvec[0,],myvec[1,],'ro')
    plt.show()
    

    Will produce:

    enter image description here

    To plot the arrays you can just slice them up into 1D vectors and plot them. I'd read the full documentation of matplotlib for all the different options. But you can treat a numpy vector as if it were a normal tuple for most of the examples.

    0 讨论(0)
  • 2020-12-13 05:28

    The suggestion in the comments by halex is correct, you want to use quiver (doc), but you need to tweak the properties a bit.

    import numpy as np
    import matplotlib.pyplot as plt
    
    soa = np.array([[0, 0, 3, 2], [0, 0, 1, 1], [0, 0, 9, 9]])
    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([-1, 10])
    ax.set_ylim([-1, 10])
    plt.draw()
    plt.show()
    
    0 讨论(0)
提交回复
热议问题