[英]Plot graph with 4 equations consist of f(x,y, xy)
I'd like to plot a graph which contains 4 functions as shown below: 我想绘制一个包含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
I started off by generating data for x and y 我首先生成x和y的数据
import numpy as np
x = np.linspace(0, 5, 100)
y = np.linspace(0, 40, 100)
But then when I tried to plot the graph, I started to confuse about how should I reformulate the equations so that it is appropriate value for x, y, xy? 但是,当我尝试绘制图形时,我开始困惑应该如何重新构造方程式,使其适合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()
You can use contour
for implicit function plotting. 您可以将
contour
用于隐式函数绘图。 By the way I don't think you can plot multiple functions with inequality on the same graph because you have to show the ranges in the 2D plane, and they overlap each other. 顺便说一句,我认为您不能在同一张图上绘制不等式的多个函数,因为您必须在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: 更新:
Seems like sympy
's plot_implicit
works so much better. 似乎
sympy
的plot_implicit
效果更好。 You may need to install it. 您可能需要安装它。
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))
Results: 结果:
you can use contourf
: 您可以使用
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"])
Here is the output: 这是输出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.