在Tkinter窗口中嵌入pyplot并更新它 [英] Embed a pyplot in a tkinter window and update it

查看:687
本文介绍了在Tkinter窗口中嵌入pyplot并更新它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,该程序在Tkinter GUI中具有pyplot(如matplotlib.pyplot),该程序可以更新.基本上我想要的是一个带有Tkinter界面的程序在pyplot上显示一些数据,然后当程序获取一些新数据时,我想更新pyplot以包含新数据.

I am trying to write a program that has a pyplot (as in matplotlib.pyplot) within a Tkinter GUI which can be updated. Basically what I want is a program with a Tkinter interface to be displaying some data on a pyplot, then when the program gets some new data I want to update the pyplot to contain the new data.

这是一个最小的示例:

import numpy as np
import Tkinter as tk

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt

root = tk.Tk()

fig = plt.figure(1)
t = np.arange(0.0,3.0,0.01)
s = np.sin(np.pi*t)
plt.plot(t,s)

canvas = FigureCanvasTkAgg(fig, master=root)
plot_widget = canvas.get_tk_widget()

def update():
    s = np.cos(np.pi*t)
    plt.plot(t,s)
    plt.draw()

plot_widget.grid(row=0, column=0)
tk.Button(root,text="Update",command=update).grid(row=1, column=0)
root.mainloop()

我希望发生的是,弹出一个包含正弦波和按钮的图形的窗口.当我按下按钮时,余弦波应出现在绘图上.

What I expect to happen is, for a window to pop up with a plot containing a sine wave and a button. When I press the button a cosine wave should appear on the plot.

当我运行程序时,实际上发生的是一个窗口弹出,其中包含一个正弦波和一个按钮.但是,当我按下按钮时,什么也没有发生.情节似乎没有更新.

What actually happens when I run the program is that a window pops up with a plot containing a sine wave and a button. However when I press the button nothing happens. The plot does not seem to be updating.

我可能会犯一些新手错误,但是我找不到在线上做这种事情的任何例子.这里出了什么问题,我怎么得到我想要的?

I'm probably making some newbie mistake but I can't find any examples online of doing this sort of thing. What is going wrong here and how would I get what I want?

任何帮助将不胜感激!

推荐答案

我已经弄清楚了,我需要在图形的canvas属性上调用draw()方法以使其重绘,更正后的代码如下.另外,任何遇到此问题或类似问题的人,如果需要动态更新自己的pyplot,都应该看一下matplotlib.animate

I figured it out, I need to call the draw() method on the figure's canvas attribute in order to get it to redraw, the corrected code is below. Also anyone who is encountering this or similar problems should probably look at matplotlib.animate if they need to be dynamically updating their pyplot

import numpy as np
import Tkinter as tk

import matplotlib
matplotlib.use('TkAgg')

from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt

root = tk.Tk()

fig = plt.figure(1)
plt.ion()
t = np.arange(0.0,3.0,0.01)
s = np.sin(np.pi*t)
plt.plot(t,s)

canvas = FigureCanvasTkAgg(fig, master=root)
plot_widget = canvas.get_tk_widget()

def update():
    s = np.cos(np.pi*t)
    plt.plot(t,s)
    #d[0].set_ydata(s)
    fig.canvas.draw()

plot_widget.grid(row=0, column=0)
tk.Button(root,text="Update",command=update).grid(row=1, column=0)
root.mainloop()

这篇关于在Tkinter窗口中嵌入pyplot并更新它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆