2014-03-27 81 views
1

我想繪製其含有4種功能的曲線圖的:與4-方程散點圖由F(X,Y,XY)

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的數據和Ÿ

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() 

回答

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

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() 

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

UPDATE:

好像sympyplot_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)) 

結果:

p1p2p3p4

相關問題