如何在等高线图中绘制线(函数)

时间:2019-06-18 04:05:37

标签: python plot line contour

我试图用两个变量绘制一个优化问题,其中我有一个目标函数和两个约束函数,每个变量都是一个不等式。我的想法是绘制一个变量相对于另一个变量的曲线,曲线将是目标函数的可能值。我要绘制的线是两个变量之间的关系(当约束等于零时)。

功能是:

目标功能

1)5000-x-y

约束

2)100 x /(x + 20)+ 100 y-10 x-10 y-50 = 0

3)3000-x-y> = 0

我使用的代码是:


from _future_ import division
from sympy import *
import math
from mpl_toolkits.mplot3d import Axes3D  # noqa: F401 unused import
import matplotlib.pyplot as plt
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np
import matplotlib.axes as Axes


"----------------------- goal"
def f1(x,y): return 5000 - (x+y)

"----------------------- constrain 1"
def g1(x,y): return 100 x / (x + 20) + 100 y - 10 x - 10 y - 50 

"----------------------- constrain2"
def h2(x): return (3000 - x)


fig = plt.figure()
ax = fig.add_subplot(111)

# Make data.
x = np.arange(0,5000, 50)
y = np.arange(0, 5000*16, 50)
x,y = np.meshgrid(x,y)


##### Goal
T = f1(x,y)
ts = ax.contour(x,y,T,15,colors='rebeccapurple')
#ax.clabel(ts, fontsize=8)

##### Constrain 1
E = g1(x,y)
es = ax.contour(mc,mh,E,15,colors='darkcyan')

##### Constrain 2
plt.plot (mc,h2(mc))

plt.rcParams['contour.negative_linestyle'] = ':'



ax.set_xlabel('x')
ax.set_ylabel('y')



plt.show()

我尝试添加约束,但没有得到代表y = 3000-x的一行,而是得到了许多不同的行。

0 个答案:

没有答案