Search code examples
pythonmatplotlibsympysolver

Solve an equation and plot the results in python


Let's assume I have an equation, such as

10 * x ** 2 - 7 * x + 3 == 2 * y + y ** 2

I want to solve it for y, then plot y vs x for x from 0 to 1. How do I do that in Python?

I know how to do it in Mathematica but I am migrating to Python. In Mathematica it's just one line

Plot[y /. Solve[10 x^2 - 7 x + 3 == 2 y + y^2, y], {x, 0, 1}]

Solution

  • With sympy's plot_implicit(), you can directly draw the equation.

    from sympy import symbols, Eq, plot_implicit
    
    x, y = symbols('x y')
    plot_implicit(Eq(10 * x ** 2 - 7 * x + 3, 2 * y + y ** 2), (x, -1, 2))
    

    sympy plot_implicit