如何清除图tkinter [英] how to Clear graph tkinter
问题描述
使用此代码,我想制作动态图形,我想每60秒使我的图形清晰可见,但是我无法使其与cla()和clf()一起使用,这是怎么回事? 除了使用cla()和clf()之外,还有其他方法可以清除图形吗?
well with this code I want to make dynamic graph, I want to make my graph clear every 60 second, but I cant make it work with cla() and clf(), what is wrong? is there any otherway to clear graph except using cla() and clf()?
#import lib client paho mqtt
from Tkinter import *
from ttk import *
from datetime import datetime
import matplotlib
import paho.mqtt.client as mqtt
import redis, time
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import style
import matplotlib.pyplot as plt
from matplotlib.pyplot import get_current_fig_manager
mqttc = mqtt.Client("serverclient",clean_session=False)#inisialisasi mqtt client
r = redis.Redis("localhost",6379)
start = time.time()
date = datetime.now().strftime('%S')
f = Figure(figsize=(5,4), dpi=100)
a = f.add_subplot(111)
b = f.add_subplot(111)
style.use('ggplot')
matplotlib.use("TkAgg")
suhu=30
cahaya=50
def mqttx():
#fungsi callback
def on_message(mqttc,obj,msg):
global LED1
global LED2
datasuhu = r.lrange("suhu",-1,-1)
datacahaya = r.lrange("cahaya",-1,-1)
print "Telah Diterima message : "+msg.payload+" topik "+msg.topic
r.rpush(msg.topic,msg.payload)
mqttc.on_message = on_message
mqttc.connect("localhost",1883)
mqttc.subscribe("suhu")
mqttc.subscribe("cahaya")
class Example(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.parent = parent
self.initUI()
self.graph()
def initUI(self):
self.parent.title("Subcriber")
self.style = Style()
self.style.theme_use("default")
self.pack(fill=BOTH, expand=1)
self.xList1 = []
self.yList1 = []
self.canvas = FigureCanvasTkAgg(f, self)
self.canvas.show()
self.canvas.get_tk_widget().pack(expand=True)
thismanager = get_current_fig_manager()
thismanager.window.wm_geometry("+500+0")
def graph(self):
suhu1 = r.lrange("suhu",-1,-1)
cahaya1 = r.lrange("cahaya",-1,-1)
date = datetime.now().strftime('%S')
join1=str(''.join(suhu1))
suhugraph=float(join1)
join2=str(''.join(cahaya1))
cahayagraph=float(join2)
self.xList1.append(date)
self.yList1.append(suhugraph)
a.clear()
a.axis([0, 100, 0, 60])
a.plot(self.xList1, self.yList1,"r-")
if date=="00" :
plt.clf()
plt.cla()
else:
self.canvas.draw()
self.after(1000, self.graph)
def main():
root = Tk()
root.geometry("1500x1400+300+300")
app = Example(root)
root.mainloop()
if __name__ == '__main__':
mqttx()
mqttc.loop_start()
main()
推荐答案
要清除图形,您必须从self.xList1
和self.yList1
To clear graph you have to remove data from self.xList1
and self.yList1
self.xList1 = []
self.yList1 = []
因为清除图(a.clear()
/plt.clf()
/plt.cla()
)之后,您仍然在列表中有数据,然后再次绘制它们.
because after clearing plot (a.clear()
/plt.clf()
/plt.cla()
) you still have data in lists and you draw them again.
我无法使用Redis
和mqtt
运行您的代码,所以我使用random
制作了版本
I can't run your code with Redis
and mqtt
so I made version with random
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import Tkinter as tk
from datetime import datetime
import random
# --- classes --- (CamelCase names)
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
# you don't need self.parent - tk.Frame sets self.master = parent
self.pack(expand=True, fill='both') # put frame inside Tk() window (and resize)
self.x_list_1 = []
self.y_list_1 = []
self.initUI()
self.draw()
def initUI(self):
self.fig = Figure(figsize=(5, 4), dpi=100)
self.a = self.fig.add_subplot(111)
self.a.axis((0, 100, 0, 60))
self.canvas = FigureCanvasTkAgg(self.fig, self)
self.canvas.get_tk_widget().pack(expand=True, fill='both')
def draw(self):
date = datetime.now().strftime('%S')
suhugraph = random.randint(1, 60)
if date == "00":
self.a.clear()
self.a.axis((0, 100, 0, 60))
self.x_list_1 = []
self.y_list_1 = []
self.x_list_1.append(date)
self.y_list_1.append(suhugraph)
self.a.plot(self.x_list_1, self.y_list_1, "r-")
self.canvas.draw()
self.after(1000, self.draw)
# --- functions --- (lower_case names)
def main():
root = tk.Tk()
app = Example(root)
root.mainloop()
# --- main ---
if __name__ == '__main__':
main()
BTW: matplotlib:清除绘图,何时使用cla(),clf()或close()?
关于调整大小后的响应-问题可能是代码中的两个循环:root.mainloop()
,可能还有mqttc.loop_start()
.我无法使用mqttc
运行代码,因此无法对其进行测试.
As for responding after resizing - problem can be two loops in code: root.mainloop()
and probably mqttc.loop_start()
. I can't run code with mqttc
so I can't test it.
顺便说一句:在没有plot()
和clear()
BTW: updating plot without plot()
and clear()
首先,您创建一个空行
self.line, = self.a.plot([], [], "r-")
然后您替换行中的所有数据
and later you replace all data in line
self.line.set_xdata( self.x_list_1 )
self.line.set_ydata( self.y_list_1 )
因此您不需要clear()
并重新设置轴.
so you don't need clear()
and set axis again.
import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
from matplotlib.figure import Figure
import Tkinter as tk
from datetime import datetime
import random
# --- classes --- (CamelCase names)
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
# you don't need self.parent - tk.Frame sets self.master = parent
self.pack(expand=True, fill='both') # put frame inside Tk() window (and resize)
self.x_list_1 = []
self.y_list_1 = []
self.initUI()
self.draw()
def initUI(self):
self.fig = Figure(figsize=(5, 4), dpi=100)
self.a = self.fig.add_subplot(111)
self.a.axis((0, 100, 0, 60))
self.canvas = FigureCanvasTkAgg(self.fig, self)
self.canvas.get_tk_widget().pack(expand=True, fill='both')
# create empty line
self.line, = self.a.plot([], [], "r-")
def draw(self):
date = datetime.now().strftime('%S')
suhugraph = random.randint(1, 60)
if date == "00":
self.x_list_1 = []
self.y_list_1 = []
self.x_list_1.append(date)
self.y_list_1.append(suhugraph)
# update data in line without `plot()` (and without `clear()`)
self.line.set_xdata( self.x_list_1 )
self.line.set_ydata( self.y_list_1 )
self.canvas.draw()
self.after(1000, self.draw)
# --- functions --- (lower_case names)
def main():
root = tk.Tk()
app = Example(root)
root.mainloop()
# --- main ---
if __name__ == '__main__':
main()
这篇关于如何清除图tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!