Python Forum

Full Version: matplotlib multithreading
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello!

I have the following problem:

I cannot live plot in a different thread, the main window (plot window) is white with a rotating doughnut mouse pointer.
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import (
    FigureCanvasTkAgg, NavigationToolbar2Tk)
import threading
from time import sleep
from tkinter import *
import random


class PlotData:

    def __init__(self, master):
        self.master = master
        master.title("PlotData")
        master.geometry("600x600")
        self.figure = Figure(facecolor='white',dpi = 95)
        self.axes = self.figure.add_subplot(111,facecolor='white')
        self.figure.subplots_adjust(left=0.08, right=0.95, top=0.96, bottom=0.1)
        
        self.xi = []
        self.yi = []
        
        self.plot_data, = self.axes.plot(self.xi,self.yi,"-o",color="coral",markersize=4, markeredgecolor='olive')
        
        self.canvas = FigureCanvasTkAgg(self.figure, master = self.master)
      
        self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
        
        self.canvas.draw()
        
        self.toolbar = NavigationToolbar2Tk(self.canvas, self.master)
        self.toolbar.update()
        
        
        threading.Thread(target=self.startPlot()).start()
    
    def startPlot(self):
        while 1:
            self.xi.append(random.randint(0,10))
            self.yi.append(random.randint(0,10))
            
            self.plot_data.set_data(self.xi, self.yi)
            sleep(1)
            print(1)


root = Tk()
my_gui = PlotData(root)
root.mainloop()
Is possible to run without blocking the window?
Thank you!