Python Matplotlib:Graphinc圆锥不等式

时间:2017-03-19 15:53:10

标签: python matplotlib

我正在尝试绘制Matplotlib中填充的圆锥曲线。理想情况下,这样的事情:

something like this:

现在我有了这段代码:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import cv2

mpl.rcParams['lines.color'] = 'k'
mpl.rcParams['axes.prop_cycle'] =mpl.cycler('color', ['k'])

x = np.linspace(-9, 9, 400)
y = np.linspace(-5, 5, 400)
x, y = np.meshgrid(x, y)

def axes():
    plt.axhline(0, alpha=.1)
    plt.axvline(0, alpha=.1)

a, b, c, d, e, f = 1, 0, 1, 0, 0, -2
axes()
plt.contour(x, y,(a*x**2 + b*x*y + c*y**2 + d*x + e*y + f), [0], colors='k')

plt.gca().set_aspect('equal')
plt.show()

它看起来像这样:

and it looks like this:

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

简单的解决方案是使用填充轮廓(contourf)而不是线轮廓(contour)。

a, b, c, d, e, f = 1, 0, 1, 0, 0, -2
plt.contourf(x, y,(a*x**2 + b*x*y + c*y**2 + d*x + e*y + f), [f, 0], colors='k')

完整示例

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-9, 9, 400)
y = np.linspace(-5, 5, 400)
x, y = np.meshgrid(x, y)

plt.axhline(0, alpha=.1, c="k")
plt.axvline(0, alpha=.1, c="k")

a, b, c, d, e, f = 1, 0, 1, 0, 0, -2

plt.contourf(x, y,(a*x**2 + b*x*y + c*y**2 + d*x + e*y + f), [f, 0], colors='k')

plt.gca().set_aspect('equal')
plt.show()

enter image description here