情节显示在jupyter笔记本中,但不显示在py文件中

时间:2019-05-01 12:54:35

标签: python tkinter jupyter tkinter-canvas

这是用于显示图的代码

fig = best.plot_sheets()

dataPlot = FigureCanvasTkAgg(fig, master = window)
dataPlot.draw()
dataPlot.get_tk_widget().pack(side='top', fill='both', expand=1)

因此,对于best.plot_sheets(),这是下面的代码:

class Sheet:
    def plot_sheet(self):
        fig,ax = plt.subplots(1)
        ax.set_xlim([0, self.W]) 
        ax.set_ylim([0, self.L]) 
        recs = []
        for i in range(len(self.rect_list)):
            if self.rect_rotate[i]:
                ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].l, self.rect_list[i].w,linewidth=3,edgecolor='r'))
            else:
                ax.add_patch(patches.Rectangle((self.rect_pos[i][0], self.rect_pos[i][1]), self.rect_list[i].w, self.rect_list[i].l,linewidth=3,edgecolor='r'))
        plt.show()

class Indiv:
    def plot_sheets(self):
        fig = plt.figure()
        col = math.ceil(len(self.sheets)**0.5)
        row = math.ceil(float(len(self.sheets))/col)
        gs = gridspec.GridSpec(row, col)
        idx = 0
        for i in range(row):
            for j in range(col):
                ax = plt.subplot(gs[i, j])
                tmp_sh = self.sheets[idx]
                ax.set_xlim([0, tmp_sh.W]) 
                ax.set_ylim([0, tmp_sh.L]) 
                for k in range(len(tmp_sh.rect_list)):
                    if tmp_sh.rect_rotate[k]:
                        ax.add_patch(patches.Rectangle((tmp_sh.rect_pos[k][0], tmp_sh.rect_pos[k][1]), tmp_sh.rect_list[k].l, tmp_sh.rect_list[k].w,linewidth=3,edgecolor='r'))
                    else:
                        ax.add_patch(patches.Rectangle((tmp_sh.rect_pos[k][0], tmp_sh.rect_pos[k][1]), tmp_sh.rect_list[k].w, tmp_sh.rect_list[k].l,linewidth=3,edgecolor='r'))
                idx = idx + 1
                if idx == len(self.sheets):
                    break
        return fig

result_list = []
for opt in packing_options:
    step_w = float(get_max(opt.sheets))
    tmp_sheets = trim_sheets(opt.sheets, max(min_w,step_w), min_l)
    tmp_l = float(get_max(tmp_sheets,False))
    tmp_result_list = []
    tmp_w = step_w
    while area/tmp_w >= tmp_l:
        tmp_sheet = Sheet(tmp_w, area/tmp_w)
        tmp_result_list.append(find_best_packing_GA(tmp_sheet, sheets_to_rect_list(tmp_sheets), \
                                                    Pm = 0, rotate_prob = 0, flex = True, only_vertical = True))
        tmp_w = tmp_w + step_w
    result_list.append(best_individual(tmp_result_list)[0])


best, avg_fit, best_index = best_individual(result_list)

它在jupyter笔记本电脑上正常工作,没有任何错误。但是,当我将文件转换为python并运行时,它没有显示图形。 jupyter文件转换为py文件时,有什么变化吗?

0 个答案:

没有答案