要清除图表,您必须从中删除数据self.xList1
and self.yList1
self.xList1 = []
self.yList1 = []
因为清除情节后(a.clear()
/plt.clf()
/plt.cla()
)列表中仍然有数据,您可以再次绘制它们。
我无法运行你的代码Redis
and mqtt
所以我制作了版本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()? https://stackoverflow.com/a/8228808/1832058
至于调整大小后的响应 - 问题可能是代码中的两个循环:root.mainloop()
或许mqttc.loop_start()
。我无法运行代码mqttc
所以我无法测试它。
顺便说一句:更新情节没有plot()
and clear()
首先创建空行
self.line, = self.a.plot([], [], "r-")
然后你替换行中的所有数据
self.line.set_xdata( self.x_list_1 )
self.line.set_ydata( self.y_list_1 )
所以你不需要clear()
并再次设置轴。
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()