With sympy https://docs.sympy.org/1.5.1/index.html,你只需这样做:
from sympy import plot_implicit, Eq
from sympy.abc import x, y
plot_implicit (Eq(3*x**2+2*x*y+4*y**2, 5))
注意Python需要**
对于幂函数,如^
被保留给按位异或 https://python-reference.readthedocs.io/en/latest/docs/operators/bitwise_XOR.html。表达式可以写成3*x**2+2*x*y+4*y**2 - 5
或使用方程运算符Eq(3*x**2+2*x*y+4*y**2, 5)
.
额外参数plot_implicit https://docs.sympy.org/1.5.1/modules/plotting.html可以设置 x 和 y 的范围,如下所示plot_implicit (3*x**2+2*x*y+4*y**2 - 5, (x, -2, 2), (y, -2, 2))
.
或者,为了获得更奇特的东西,matplotlibsimshow
可以在 x,y 方向上绘制由 z 值着色的完整区域。选择一个发散色彩图 https://matplotlib.org/3.1.0/tutorials/colors/colormaps.html#diverging,椭圆将显示在由 a 指示的中心 z 值处分歧规范 https://matplotlib.org/3.1.0/gallery/userdemo/colormap_normalizations_diverging.html.
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.colors as mcolors
xmin, xmax = -2, 2
ymin, ymax = -2, 2
x, y = np.meshgrid(np.linspace(xmin, xmax, 500), np.linspace(ymin, ymax, 500))
z = 3*x**2+2*x*y+4*y**2
divnorm = mcolors.DivergingNorm(vmin=z.min(), vcenter=5, vmax=z.max())
#plt.contourf(x, y, z, levels=15, norm=divnorm, cmap='seismic')
plt.imshow(z, interpolation='bilinear', norm=divnorm, cmap='seismic', origin='lower', extent=[xmin, xmax, ymin, ymax])
plt.colorbar()
plt.show()