Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot arbitrary 2-D function in python/pyplot like Matlab's Ezplot

I'm looking for a way to generate a plot similar to how ezplot works in MATLAB in that I can type:

ezplot('x^2 + y^2 = y + 5')

and get a graph ready to go for any arbitrary function. I'm only worrying about the case where I have both a x and a y.

I only have the function, and I'd really rather not go about trying to calculate all the y values for some given x range if I didn't have to.

The few solutions I've seen suggested are either about decision boundaries (which this is not. There is no test data or anything, just an arbitrary function) or are all for functions already defined as y = some x equation which doesn't really help me.

I would somewhat accept if there was a good way to mimic Wolfram|Alpha in their solve functionality("solve x^2 + y^2 = y + 5 for y" will give me two functions I could then graph separately), but rather prefer the ezplot as that's more or less instant within MATLAB.

like image 550
MasterOdin Avatar asked Jan 06 '16 16:01

MasterOdin


1 Answers

I think you could use sympy plotting and parse_expr for this For your example, this would work as follows

from sympy.plotting import plot_implicit
from sympy.parsing.sympy_parser import parse_expr

def ezplot(s):
    #Parse doesn't parse = sign so split
    lhs, rhs = s.replace("^","**").split("=")
    eqn_lhs = parse_expr(lhs)
    eqn_rhs = parse_expr(rhs)

    plot_implicit(eqn_lhs-eqn_rhs)

ezplot('x^2 + y^2 = y + 5')

This can be made as general as needed

like image 96
Ed Smith Avatar answered Sep 18 '22 23:09

Ed Smith