繁体   English   中英

如何在tkinter的同一张图上画多条线?

[英]How to draw multiple lines on the same graph in tkinter?

我是 tkinter 的新手,我需要能够在同一张图表上绘制多条线 plot,每条线都有自己的图例。 我可以在 matplotlib 中使用 show () 执行此操作,但随后图形与 GUI window 分开。我的代码从我创建的文件列表中获取文件,然后将它们绘制在屏幕上,但不断覆盖每个图形 - 有没有办法这样做是为了让所有折线图都出现在 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()

output

# use tkchart module....
# https://pypi.org/project/tkchart/ 
# https://github.com/Thisal-D/tkchart


import tkchart
import tkinter

root = tkinter.Tk()

#create chart
chart_1 = tkchart.LineChart(master=root,width=1000 ,height=600 
                         ,chart_line_len=80 ,sections=True 
                         ,sections_count=10 ,values_labels=True  
                         ,values_labels_count=5 ,
                         max_value = 100)
chart_1.pack()


#create lines  for chart
line_1 = tkchart.Line(master=chart_1 ,color="#00ff00" ,height=4)
line_2 = tkchart.Line(master=chart_1 ,height=4 ,color ="#ffff00")

value = [x for x in range(0,100)]
import random
def display():
   chart_1.display(line=line_1 ,values=random.choices(value))
   chart_1.display(line=line_2 ,values=random.choices(value))
   root.after(500,display)
display()


root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM