Du bist nicht eingeloggt.

Login

Pass

Registrieren

Community
Szene & News
Locations
Impressum

Forum / Bits und Bytes

Python Plot live aktualisieren

Inception_ - 31
Profi (offline)

Dabei seit 05.2009
621 Beiträge

Geschrieben am: 26.10.2020 um 12:27 Uhr
Zuletzt editiert am: 26.10.2020 um 12:30 Uhr

Hi zusammen.

Ich möchte in Python einen Live-Plot haben (siehe Animation unten). Das ganze funktioniert bisher, jedoch nur in einem separaten Figure Window. Ich möchte nun diesen "animierten" Plot mit "canvas" auf eine "tkinter" GUI bringen. Beim Ausführen wartet er aber immer ab, bis die komplette Schleife durchlaufen ist und plotted dann nur das Gesamtergebnis. Ich sehe keine Live-Updates. Eine Idee, wie man das hinbekommt?

Ohne die tkinter GUI kann man das erreichen mit matplotlib.pyplot.pause(). Wenn ich das bei canvas anwende, öffnet er an der Stelle aber wieder ein separates Figure. Er updated den Plot nicht in der GUI.

[verlinkte Grafik wurde nicht gefunden]
(Animation gehört nicht zum Code unten!)

Zitat:

from tkinter import *
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
import matplotlib.pyplot as plt
import time

root = Tk()

figId = plt.figure(1)
plot1 = plt.subplot(211)
plot2 = plt.subplot(212)
plt.draw()

canvas = FigureCanvasTkAgg(figId, master=root)
canvas.get_tk_widget().pack()

vals1 = [5, 6]
vals2 = [-3, 9]

i = 0
while(i<2):
plot1.plot(i,vals1[i],'x')
plot2.plot(i,vals2[i],'x')
i += 1
time.sleep(2)


Science, 128√e980 .. :*

Inception_ - 31
Profi (offline)

Dabei seit 05.2009
621 Beiträge

Geschrieben am: 26.10.2020 um 15:26 Uhr
Zuletzt editiert am: 26.10.2020 um 15:35 Uhr

Mehr als drei Stunden hat es gedauert. Das ist die Lösung:

Zitat:

import random
from itertools import count
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)

x_vals = []
y_vals1 = []
y_vals2 = []

index = count()

root = Tk()
figId = plt.figure()
canvas = FigureCanvasTkAgg(figId, master=root)
canvas.get_tk_widget().pack()
canvas.draw()

def animate(i):
x_vals.append(next(index))
y_vals1.append(random.randint(0, 5))
y_vals2.append(random.randint(0, 5))
plt.cla()
plt.plot(x_vals, y_vals1)
plt.plot(x_vals, y_vals2)

ani = FuncAnimation(plt.gcf(), animate, interval=1000)

root.mainloop()

[zustimmen2 Personen stimmen diesem Beitrag zu:
deathworm, tracer, ... mehr anzeigen


Science, 128√e980 .. :*

zetisonu - 24
Anfänger (offline)

Dabei seit 04.2023
7 Beiträge
Geschrieben am: 10.04.2023 um 10:04 Uhr
Zuletzt editiert am: 10.04.2023 um 10:05 Uhr

Zitat von Inception_:

Mehr als drei Stunden hat es gedauert. Das ist die Lösung:

Zitat:

import random
from itertools import count
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from tkinter import *
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)

x_vals = []
y_vals1 = []
y_vals2 = []

index = count()

root = Tk()
figId = plt.figure()
canvas = FigureCanvasTkAgg(figId, master=root)
canvas.get_tk_widget().pack()
canvas.draw()

def animate(i):
x_vals.append(next(index))
y_vals1.append(random.randint(0, 5))
y_vals2.append(random.randint(0, 5))
plt.cla()
plt.plot(x_vals, y_vals1)
plt.plot(x_vals, y_vals2)

ani = FuncAnimation(plt.gcf(), animate, interval=1000)

root.mainloop()


I really enjoyed reading this article on chess online modern living, which provides a nuanced analysis of how we can balance our desire for convenience with our need for connection.
JosephThomas - 28
Anfänger (offline)

Dabei seit 07.2023
1 Beitrag
Geschrieben am: 14.07.2023 um 09:43 Uhr
Zuletzt editiert am: 14.07.2023 um 09:43 Uhr

gold miner is a game that mixes resource management with fast reflexes, and it challenges players to explore the depths of the soil in order to discover the strategies behind profitable gold mining.
seram - 34
Fortgeschrittener (offline)

Dabei seit 05.2022
81 Beiträge
Geschrieben am: 02.08.2023 um 22:05 Uhr

hast du es geschafft?
Inception_ - 31
Profi (offline)

Dabei seit 05.2009
621 Beiträge

Geschrieben am: 02.08.2023 um 23:51 Uhr

Zitat von seram:

hast du es geschafft?


Siehe zweiter Post.

Science, 128√e980 .. :*

rakhisharmax - 25
Anfänger (offline)

Dabei seit 08.2023
8 Beiträge
Geschrieben am: 07.08.2023 um 13:56 Uhr

Our school accompanies know how to fulfill the desire of each and every man they meet during the evening.Escorts Goa On the off chance that you are searching for some hot fun with our hot young ladies around you, you want to call us now and get yourself a casual sexual encounter with an escort.

  [Antwort schreiben]

Forum / Bits und Bytes

(c) 1999 - 2024 team-ulm.de - all rights reserved - hosted by ibTEC Team-Ulm

- Presse - Blog - Historie - Partner - Nutzungsbedingungen - Datenschutzerklärung - Jugendschutz -