Drawing phase space trajectories with arrows in matplotlib

前端 未结 1 1593
陌清茗
陌清茗 2021-02-04 10:18

I am trying to draw the phase space plot for a certain dynamical system. In effect, I have a 2d plane in which there is a starting point followed by next point and so on. I want

1条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 10:53

    I think a solution would then look like this:

    Using that code:

    import numpy as np
    import matplotlib.pylab as plt
    from scipy.integrate import odeint
    from scipy.misc import derivative
        
    def system(vect, t):
        x, y = vect
        return [x - y - x * (x**2 + 5 * y**2), x + y - y * (x**2 + y**2)]
        
    vect0 = [(-2 + 4*np.random.random(), -2 + 4*np.random.random()) for i in range(5)]
    t = np.linspace(0, 100, 1000)
    
    color=['red','green','blue','yellow', 'magenta']
        
    plot = plt.figure()
        
    for i, v in enumerate(vect0):
        sol = odeint(system, v, t)
        plt.quiver(sol[:-1, 0], sol[:-1, 1], sol[1:, 0]-sol[:-1, 0], sol[1:, 1]-sol[:-1, 1], scale_units='xy', angles='xy', scale=1, color=color[i])    
    
    plt.show(plot)    
    

    [EDIT: Some explanation on indices:

    • A definition of quiver and its arguments can be found here: https://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.quiver
    • Good examples for quiver can be found here: https://www.getdatajoy.com/examples/python-plots/vector-fields
    • quiver requires vectors as inputs, which are defined by a start and end points (start and end points are basically points i and i+1 from the line coordinates stored in sol)
    • As a consequence, the length of the vector array will be one shorter than the length of the coordinate array
    • In order to compensate for that and to provide arrays with the same length for coordinates and vectors to quiver, we have to play with indices as follows:
    • sol[:-1, 0] (:-1 in first index drops the last coordinate)
    • sol[1:, 0] (1: in first index starts drops first coordinate)
    • sol[1:, 0] - sol[:-1, 0] is therefore a convenient way to create two vectors of length n-1 and subtract them in a way that the result is sol[i+1] - sol[i]

    0 讨论(0)
提交回复
热议问题