【发布时间】:2021-08-04 21:02:16
【问题描述】:
我正在为大学做一个项目,我有以下代码,它几乎可以完成这项工作。但是,如果我能更进一步并将生成的图形放在 tkinter 窗口中,那就太好了。如果您有任何想法,我很想听听您的想法。提前感谢您的帮助。
import numpy as np
from numpy import random as rd
import matplotlib.cm as cm
import matplotlib.pyplot as plt
#-----------------------------------------------------------------------------------------------------------------------
# Ingredients A & B needed for product 1
A1 = 3
B1 = 8
Shell1 = 100
# Ingredients A & B needed for product 2
A2 = 6
B2 = 4
Shell2 = 125
# Total Capacity of units A & B
CA = 30
CB = 44
# Maximum Production Capacity of 1 & 2
C1 = 5
C2 = 4
#-----------------------------------------------------------------------------------------------------------------------
# Define Optimization Functions
def objective(X,Y):
return Shell1*X+Shell2*Y
def constrain1(X,Y):
return CA-A1*X-A2*Y
def constrain2(X,Y):
return CB-B1*X-B2*Y
def constrain3(X,Y):
return C1-X
def constrain4(X,Y):
return C2-Y
#-----------------------------------------------------------------------------------------------------------------------
maxx = int(min(CA/A1,CB/B1))
maxy = int(min(CA/A2,CB/B2))
x = np.linspace(0, maxx+1, maxx+2)
y = np.linspace(0, maxy+1, maxy+2)
X, Y = np.meshgrid(x, y)
# Generate Contour Plot
fig, ax = plt.subplots()
cpm = ax.contourf(objective(X,Y), 10, alpha=.35, cmap = cm.turbo)
cpl = ax.contour(objective(X,Y), 10, colors='black', linewidths=0.2)
cbar = plt.colorbar(cpm)
cbar.set_label('Objective Function Contour Plot', rotation=270, labelpad=20, fontsize=14)
#-----------------------------------------------------------------------------------------------------------------------
# Identify Constrains
c1 = ax.contour(constrain1(X,Y),[0],colors='red', linewidths=3)
c2 = ax.contour(constrain2(X,Y),[0],colors='red', linewidths=3)
c3 = ax.contour(constrain3(X,Y),[0],colors='red', linewidths=3)
c4 = ax.contour(constrain4(X,Y),[0],colors='red', linewidths=3)
ax.clabel(c1, fmt='Constrain 1', manual=[(1,4)], fontsize=9)
ax.clabel(c2, fmt='Constrain 2', manual=[(3,5)], fontsize=9)
ax.clabel(c3, fmt='Constrain 3', manual=[(5,5)], fontsize=9)
ax.clabel(c4, fmt='Constrain 4', manual=[(4.3,4)], fontsize=9)
#-----------------------------------------------------------------------------------------------------------------------
# Potential Feasible Solution
rx = int((rd.rand()) * 5)
ry = int((rd.rand()) * 5)
while constrain1(rx,ry)<0 or constrain2(rx,ry)<0 or constrain3(rx,ry)<0 or constrain4(rx,ry)<0:
rx = int((rd.rand()) * 5)
ry = int((rd.rand()) * 5)
continue
ax.plot(rx,ry,marker='X',color='goldenrod',ms=8,label='Potential FS')
ax.contour(objective(X,Y),[objective(rx,ry)],colors='k', linewidths=2)
#-----------------------------------------------------------------------------------------------------------------------
# Maximum & Minimum Feasible Solution
MAX = MIN = objective(rx,ry)
imax = imin = rx
jmax = jmin = ry
for i in x:
for j in y:
if constrain1(i,j)>=0 and constrain2(i,j)>=0 and constrain3(i,j)>=0 and constrain4(i,j)>=0:
# All Feasible Solutions
ax.plot(i,j,marker='+',color='k',ms=10)
if objective(i,j)>MAX:
MAX = objective(i,j)
imax = int(i)
jmax = int(j)
if objective(i,j)<MIN:
MIN = objective(i,j)
imin = int(i)
jmin = int(j)
ax.plot(imax,jmax,marker='s',color='dodgerblue',ms=8,label='Max Objective FS')
ax.plot(imin,jmin,marker='s',color='deeppink',ms=8,label='Min Objective FS')
#-----------------------------------------------------------------------------------------------------------------------
# Activate Grid
grid = ax.grid(linestyle='dashed')
# Plot Labels
plt.xlabel('Units of Product 1',fontsize=12)
plt.ylabel('Units of Product 2',fontsize=12)
plt.legend(bbox_to_anchor=(0., 1.02, 1, 0), loc='lower left', ncol=2, mode="expand", borderaxespad=0)
plt.show()
#-----------------------------------------------------------------------------------------------------------------------
# Print Results
print('\n' + '\033[35m' + 'Potential Feasible Solution: ' + '\033[0m' + str(rx) + '\033[34m' + ' Units of Product 1, '
+ '\033[0m' + str(ry) + '\033[31m' + ' Units of Product 2: ' + '\033[33m' + 'Revenue = ' + '\033[0m'
+ str(objective(rx,ry)))
print('\n' + '\033[35m' + 'Maximum Feasible Solution: ' + '\033[0m' + str(imax) + '\033[34m' + ' Units of Product 1, '
+ '\033[0m' + str(jmax) + '\033[31m' + ' Units of Product 2: ' + '\033[33m' + 'Revenue = ' + '\033[0m'
+ str(objective(imax,jmax)))
print('\n' + '\033[35m' + 'Minimum Feasible Solution: ' + '\033[0m' + str(imin) + '\033[34m' + ' Units of Product 1, '
+ '\033[0m' + str(jmin) + '\033[31m' + ' Units of Product 2: ' + '\033[33m' + 'Revenue = ' + '\033[0m'
+ str(objective(imin,jmin)))
【问题讨论】:
-
你看过教程吗?我很确定互联网上的
tkinterwindows 中有不少matplotlib图表的例子。 -
我有。问题是他们几乎都使用 Pandas,而我没有,这有点令人困惑。
-
另外,他们中的许多人从头开始为图形创建 def 或类。我想做的是创建一个根,它将从我已经使用的命令中继承图形(我什至不确定这是否可能)。
-
恐怕我不这么认为。就像我在其他教程中看到的那样,创建单独的定义和类。无论如何感谢您的帮助。
-
创建一个
tkinter窗口,然后使用canvas = FigureCanvasTkAgg(fig, master=root)。之后你可以调用canvas.get_tk_widget().pack()和canvas.draw()在tkinter窗口中显示
标签: python matplotlib tkinter