带有4个方程的绘图图由f(x,y,xy)组成

时间:2014-03-27 01:12:55

标签: python graph matplotlib

我想绘制一个包含4个函数的图形,如下所示:

 xy1: - 12.8 x - 0.108 y + xy >= -1.3824
 xy2: - 40 x - 5 y + xy >= -200
 xy3: - 40 x - 0.108 y + xy <= -4.32
 xy4: - 12.8 x - 5 y + xy <= -64

我开始为x和y生成数据

 import numpy as np
 x = np.linspace(0, 5, 100)
 y = np.linspace(0, 40, 100)

但是当我试图绘制图形时,我开始混淆我应该如何重新制定方程式,以便它对x,y,xy来说是合适的值?

 import matplotlib.pyplot as plt
 plt.plot((-1.3824 + (12.8 * x) + (0.108 * y)), y)   <--- this doesn't seems to be right
 plt.show()

2 个答案:

答案 0 :(得分:1)

您可以使用contour进行隐式函数绘图。顺便说一下,我不认为你可以在同一个图上用不等式绘制多个函数,因为你必须在2D平面上显示范围,并且它们相互重叠。

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(0, 5, 100)
y = np.linspace(0, 40, 100)

X, Y = np.meshgrid(x, y)

XY1 = (-12.8*X - 0.108*Y + X*Y) >= -1.3824
XY2 = - 40*X - 5*Y + X*Y >= -200
XY3 = - 40*X - 0.108*y + X*Y <= -4.32
XY4 = - 12.8*X - 5*Y + X*Y <= -64

plt.contour(X,Y, XY1)
plt.contour(X,Y, XY2)
plt.contour(X,Y, XY3)
plt.contour(X,Y, XY4)
plt.show()

=============================================== =================

更新:

似乎sympy&#39; s plot_implicit的效果要好得多。您可能需要安装它。

from sympy import plot_implicit, symbols, And
x, y = symbols('x y') 

p1 = plot_implicit(And(-12.8*x - 0.108*y + x*y >= -1.3824), (x,0,5), (y,0,40))
p2 = plot_implicit(And(-40*x - 5*y + x*y >= -200), (x,0,5), (y,0,40))
p3 = plot_implicit(And(-40*x - 0.108*y + x*y <= -4.32), (x,0,5), (y,0,40))
p4 = plot_implicit(And(-12.8*x - 5*y + x*y <= -64), (x,0,5), (y,0,40))

结果:

p1 p2 p3 p4

答案 1 :(得分:0)

您可以使用contourf

import pylab as pl
import numpy as np

x, y = np.mgrid[-10:10:100j, -10:50:100j]
z1 = - 12.8 * x - 0.108 * y + x * y + 1.3824
z2 = - 40 * x - 5 * y + x * y + 200
z3 = - 40 * x - 0.108 * y + x * y + 4.32
z4 = - 12.8 * x - 5 * y + x * y + 64

fig, axes = pl.subplots(2, 2, figsize=(12, 8))
axes = axes.ravel()
axes[0].contourf(x, y, z1, levels=[0, 1e10], alpha=0.2, colors=["blue"])
axes[1].contourf(x, y, z2, levels=[0, 1e10], alpha=0.2, colors=["green"])
axes[2].contourf(x, y, z3, levels=[-1e10, 0], alpha=0.2, colors=["red"])
axes[3].contourf(x, y, z4, levels=[-1e10, 0], alpha=0.2, colors=["yellow"])

这是输出:

enter image description here