gut mit diesem Code Ich möchte dynamische Grafik machen, möchte ich meine Grafik löschen alle 60 Sekunden, aber ich kann es nicht mit cla() und clf() arbeiten, was ist los? Gibt es einen anderen Weg, um Graphen zu löschen, außer mit cla() und clf()?wie man Graph tkinter
#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()
BTW: Sie haben keine Sekunden in Zeichenfolge zu konvertieren - 'datetime.now() second' – furas
' '' .join() 'String erstellt, so dass Sie benötigen' str nicht() '. - 'suhugraph = float (''. beitreten (suhu1))' – furas
@furas oh ja danke Kumpel, aber können Sie mir mit meinem Problem helfen? Kann Matplotlib Abbildung/Leinwand hier nicht klar –