在Python中绘制高阶隐式多项式函数

时间:2020-04-16 05:25:51

标签: python matplotlib

使用Python,是否可以在 x y 中绘制隐式方程式?

例如,
x³y²+ 3 x y + 9 x +x⁴y⁴+x⁴+ 3 = 0

或者使用类似Python的符号

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

1 个答案:

答案 0 :(得分:0)

您可以使用外部库绘制隐式函数 SymPy,即使用 sympy.plot_implicit依次使用Matplotlib 引擎盖:

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

另一种方法(比SymPy的使用少直接)是扩展Matplotlib中plt.contour的使用

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>

(请注意(0,)是一个包含要为其绘制等值线的所有值的元组)

enter image description here