Vaasuawesome7
Vaasuawesome7

Reputation: 1

Plotting high order, implicit polynomial functions in Python

Using Python, is it possible to plot implicit equations in x and y?

E.g.,
                                  x³ y² + 3 x y + 9 x + x⁴ y⁴ + x⁴ + 3 = 0

or, using a Python-like notation

x**3*y**2 + 3*x*y + 9*x + x**4*y**4 + 3 = 0

Upvotes: 0

Views: 325

Answers (1)

gboffi
gboffi

Reputation: 25023

You can plot an implicit function using the external library SymPy, namely using sympy.plot_implicit that in turn uses Matplotlib under the hood:

from sympy import symbols, plot_implicit
x, y = symbols('x y')
plot_implicit(x**3*y**2+3*x*y+9*x+x**4*y**4+x**4+3, x, y)

enter image description here

Another option, less direct than the use of SymPy, is to stretch the usage of plt.contour in Matplotlib

In [1]: import matplotlib.pyplot as plt 
   ...: import numpy as np 
   ...:  
   ...: %matplotlib 
   ...:  
   ...: X = np.linspace(-4, 4, 201) 
   ...:  
   ...: x, y = np.meshgrid(X, X)                                                          

In [2]: z = x**3*y**2+3*x*y+9*x+x**4*y**4+x**4+3                                          

In [3]: plt.contour(x, y, z, (0,))                                                        
Out[3]: <matplotlib.contour.QuadContourSet at 0x7fb866c805d0>

(note that (0,) is a tuple containing all the values for which you want to draw an isoline)

enter image description here

Upvotes: 1

Related Questions