首页 > 解决方案 > 如何在 tkinter 的同一张图上绘制多条线?

问题描述

我是 tkinter 的新手,我需要能够在同一个图表上绘制多个线图,每个线图都有自己的图例。我可以使用 show () 在 matplotlib 中执行此操作,但随后图形与 GUI 窗口分离。我的代码从我创建的文件列表中获取文件,然后将它们绘制在屏幕上,但不断覆盖每个图表 - 有没有办法做到这一点,以便所有折线图出现在 tkinter 的同一个图表上?

    for item in myfile_list:
        x, y = np.loadtxt(item + '_' + 'Test.csv', skiprows=1, usecols=[x_axis_column, y_axis_column],
                          unpack=True, delimiter=',')
        # graph size in inches
        fig = Figure(figsize=(5, 5))

        a = fig.add_subplot(111)
        a.plot(x, y, color='blue')
        a.set_title("Title", fontsize=16)
        a.set_ylabel("Y", fontsize=14)
        a.set_xlabel("X", fontsize=14)
        canvas = FigureCanvasTkAgg(fig, master=chart_frame)
        # place graph in first row and column of chart_frame
        canvas.get_tk_widget().grid(row=0, column=0)
        canvas.draw()
        toolbar_frame = Frame(plot_frame)
        toolbar_frame.grid(row=1, column=0)
        toolbar = NavigationToolbar2Tk(canvas, toolbar_frame)
        toolbar.update()

标签: pythontkinterplot

解决方案


推荐阅读