在tkinter帧中更新Matplotlib条形图

2024-10-02 18:22:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试更新tkinter中的图表。 要在tkinter中交换窗口,我使用按钮和pack_-forget() 但当重新打开图表时,我有以下信息:

MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance.  In a future version, a new instance will always be created and returned.  Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

每次图形数据更改时,都必须对其进行更新。我知道我必须使用plt.clear()之类的东西来删除一个新的脚本,重新初始化绘图,但我不知道如何进行,你能帮我吗

#########GRAPH FRAME CONTENT##########
matplotlibframe = Frame(root,bd=2)
fig = Figure(figsize = (9, 15),dpi = 80)
canvas = FigureCanvasTkAgg(fig,master = matplotlibframe)

Return_Btn = Button(matplotlibframe,text='Return',command=machine)

def checkstock():
    global canvas
    global fig
    hide_frame()
    matplotlibframe.pack()
    #Tell server i want to check the stock
    tell_Stock = pickle.dumps("Graph")
    client_socket.sendall(tell_Stock)
    #Getting the stock data
    Stock_status = client_socket.recv(1024)
    Stock_status = pickle.loads(Stock_status)
    
    plot1 = fig.add_subplot(111)

    canvas.draw()

    x = []
    y = []
    colors = []
    for item in range(len(Stock_status)):
        x.append(Stock_status[item][0])
        y.append(Stock_status[item][1])

    for loop in range(9):
        if (loop % 2) == 0:
            colors.append('#FCAB64') 
        else:
            colors.append('#A1FCDF')

    plot1.barh(x,y,height=0.5,color=colors)
    plot1.set_title('Stock')

    canvas.get_tk_widget().pack(side=LEFT)

    Return_Btn.pack(side=RIGHT)

machine()包含很多东西。 隐藏我的GUI的所有其他框架的\u frame()contain.pack\u forget() 要交换帧,我需要执行以下操作: 在机器()中:

def machine():
    #plt.close(fig)
    hide_frame()
    frame.pack(side=LEFT,fill="none",expand=TRUE)
    frameRight.pack(side=LEFT,fill="none",expand=TRUE,ipadx=50)

在隐藏框架()中:

def hide_frame():
    frame.pack_forget()
    frameRight.pack_forget()
    frameConfirm.pack_forget()
    matplotlibframe.pack_forget()
    canvas.get_tk_widget().pack_forget()
    framePay.pack_forget()

Tags: theinstancereturnstatusstockfigframeside