首页 > 解决方案 > 绘图显示在 jupyter 笔记本中,但未显示在 py 文件中

问题描述

这是显示情节的代码

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 notebook 上运行良好,没有任何错误。但是当我将文件转换为 python 并运行时,它不会显示图表。当 jupyter 文件转换为 py 文件时有什么变化吗?

标签: pythontkinterjupytertkinter-canvas

解决方案


推荐阅读