Using Sympy Equations for Plotting

后端 未结 2 1306
眼角桃花
眼角桃花 2020-12-05 11:37

What is the best way to create a Sympy equation, do something like take the derivative, and then plot the results of that equation?

I have my symbolic equation, but

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

    Using SymPy

    You can use directly the plotting functions of SymPy:

    from sympy import symbols
    from sympy.plotting import plot as symplot
    
    t = symbols('t')
    x = 0.05*t + 0.2/((t - 5)**2 + 2)
    symplot(x)
    

    Most of the time it uses matplotlib as a backend.

    0 讨论(0)
  • 2020-12-05 12:33

    You can use numpy.linspace() to create the values of the x axis (x_vals in the code below) and lambdify().

    from sympy import symbols
    from numpy import linspace
    from sympy import lambdify
    import matplotlib.pyplot as mpl
    
    t = symbols('t')
    x = 0.05*t + 0.2/((t - 5)**2 + 2)
    lam_x = lambdify(t, x, modules=['numpy'])
    
    x_vals = linspace(0, 10, 100)
    y_vals = lam_x(x_vals)
    
    mpl.plot(x_vals, y_vals)
    mpl.ylabel("Speed")
    mpl.show()
    

    (improvements suggested by asmeurer and MaxNoe)

    Alternatively, you can use sympy's plot():

    from sympy import symbols
    from sympy import plot
    
    t = symbols('t')
    x = 0.05*t + 0.2/((t - 5)**2 + 2)
    
    plot(x, (t, 0, 10), ylabel='Speed')
    
    0 讨论(0)
提交回复
热议问题