Schedule an arbitrary 2-D function in python / piplo like Matlab Ezplot

I am looking for a way to create a graph similar to how ezplot works in MATLAB, in which I can print:

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

and get a graph ready for any arbitrary function. I am only worried about where I have x and y.

I only have a function, and I would rather not try to calculate all y values ​​for some given range of x if I don't need it.

The few solutions that I saw are either the boundaries of the solution (that’s not true. There is no test data or anything, just an arbitrary function) or all for functions already defined as y = some x equation, which is not I will really help me .

I would agree if there was a good way to imitate Wolfram | Alpha in their decisive functionality ("solve x ^ 2 + y ^ 2 = y + 5 for y" will give me two functions that I could then graphically separate), but rather prefer ezplot as more or less instant in MATLAB.

+5
source share
3 answers

I think you could use sympy plotting and parse_expr for this. For your example, this will 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 done as general as necessary.

+7
source

You can use sympy to solve the equation, and then use the resulting functions to build y over x:

 import sympy x=sympy.Symbol('x') y=sympy.Symbol('y') f = sympy.solve(x**2 + y**2 - y - 5, [y]) print f xpts = (numpy.arange(10.)-5)/10 ypts = sympy.lambdify(x, f, 'numpy')(xpts) # then eg: pylab.scatter(xpts, ypts) 
0
source
Decision

@EdSmith works great. However, I have another suggestion. You can use the contour graph. You can rewrite your function as f(x, y)=0 and then use this code

 from numpy import mgrid, pi import matplotlib.pyplot as plt def ezplot(f): x, y = mgrid[-2*pi:2*pi:51, -2*pi:2*pi:51] z = f(x, y) ezplt = plt.contour(x, y, f, 0, colors='k') return ezplt 

This is the main idea. Of course, you can generalize it as a function in MATLAB, for example, common intervals x and y , passing the function as a string, etc.

0
source

Source: https://habr.com/ru/post/1240011/


All Articles