Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
424 views
in Technique[技术] by (71.8m points)

python - How to update a graph created by matplotlib in tkinter

I have an app created by using tkinter. There is just a window in which I'm typing X and Y. Having these x and y, my app should create a graph using matplotlib.

First time it works correctly, but others no. I mean, I open the app, input X and Y, click button and the app creates graph, then I input new X and Y, click button and this doesn't work, graph is the same, not new. I've searched many sites, but no one of solutions works.

a part of code looks like:

from tkinter import *
import matplotlib

import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure


window_width = 1000
window_height = 600
root = Tk()

global_fig = None


def create_draw(x_dots, y_dots):
    global global_fig
    if global_fig is not None:
        global_fig.clf()

    global_fig = Figure(figsize=(5, 4), dpi=100)

    global_fig.add_subplot(111).plot(x_dots, y_dots)

    canvas = FigureCanvasTkAgg(global_fig, master=root)  # A tk.DrawingArea.
    canvas.draw()
    canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)

    toolbar = NavigationToolbar2Tk(canvas, root)
    toolbar.update()
    canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
    root.update()
    global_fig.canvas.draw()

root.mainloop()

the sense of problem is:

root.mainloop()
create_draw([3,4], [2,3])

after typing it I see a graph, then I type

create_draw([-1, -2], [-3, -4])

And I should see another graph, but I see old graph in tkinter.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To reuse a plotting area, you need to:

  1. clear the current figure
  2. Add new data to the plot
  3. Redraw the canvas

Below is how it can work with some changes to your code:

from tkinter import *
import numpy as np

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2Tk
from matplotlib.figure import Figure

window_width = 1000
window_height = 600

root = Tk()

fig = Figure(figsize=(5, 4), dpi=100)

canvas = FigureCanvasTkAgg(fig, master=root)  # A tk.DrawingArea.
canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
toolbar = NavigationToolbar2Tk(canvas, root)

def draw_chart():
    fig.clear()
    fig.add_subplot(111).plot(np.random.randint(1,10,5), np.random.randint(10,20,5)) #generate random x/y
    canvas.draw_idle()

Button(root,text="Draw",command=draw_chart).pack()

root.mainloop()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...