How do I update a matplotlib plot in realtime and also have a REPL?

I am in a situation where I have a primitive REPL with some simple commands. These commands generate data and put the data into a threadsafe queue. I then have a function that takes the data from the queue and plots it on a matplotlib plot.

What I would ultimately like to do is to be able to have a REPL command that creates a thread that generates data upon starting and puts the data into the queue. The data would then be plotted as it arrives from the queue into a matplotlib plot. I am having trouble understanding how to implement this.

My first idea was to just implement the REPL as while loop in the main thread and then have the function with matplotlib run in a separate thread. Unfortunately, matplotlib seems to require running in the main thread of the application. So I could not pursue that.

The next thing I tried was to put the REPL into its own thread and keep a matplotlib function in the main thread. Here’s my main.py:

import threading
from queue import Queue
from typing import List

from repl import REPL
from stripchart import live_stripchart


def main():

    queue :Queue[List[float]] = Queue(maxsize = 0)
    quit: threading.Event = threading.Event()

    # A primitive REPL to control application. It runs
    # in a separate thread. And puts data into the queue.
    repl = REPL(queue, quit)
    repl.start()

    # live_stripchart is a function that plots data from the queue.
    # It runs in the main thread (because matplotlib seems to require this?)
    live_stripchart(queue, quit) # 
    repl.join()
    

if __name__ == "__main__":
    main()

The REPL consists of a simple while loop that runs in a separate thread. I feel like this is a problem. Is it OK to use input statements that take data from keyboard input in a separate thread? ¯_(ツ)_/¯, but it seems like it works.

import re
import threading
from queue import Queue
from typing import List
import random

fake_data_cmd_regex :re.Pattern = re.compile(r'^fake_datas+-ns*(d+)s*$')


class REPL(threading.Thread):

    def __init__(self, queue :Queue[List[float]], quit :threading.Event):
        self.queue :Queue[List[float]] = queue
        self.quit = quit
        super().__init__()


    def run(self):
        try:
            # Here's a primitive REPL to contol the application
            while True:
                command = input('> ')

                if command == 'help':
                    print("quit")
                    print("    Exit the application.")
                    print("fake_data -n <number>")
                    print("    Generate some data and put it in queue.")
                    continue

                if command == 'quit':
                    self.quit.set() # fire quit event to stop theads that check it.
                    break           # exit this loop, terminating this thread.

                match = fake_data_cmd_regex.match(command)
                if match:
                    n, = match.groups()
                    print(f"generating {n} fake data points...")
                    self.queue.put([random.random() for _ in range(int(n))])
                    continue 

                if command == '':
                    continue
                
                print("Unknown command. Type 'help' for help.")

        except KeyboardInterrupt:
            self.quit.set() # stop well monitor if it's running

    
        print(f"cli done")  

Finally, I have the stripchart, this takes the data from the queue and plots it. This function runs in the main thread of the program.

import matplotlib.pyplot as plt
import threading
from queue import Queue, Empty
from typing import List

def live_stripchart(queue: Queue[List[float]], quit: threading.Event):

    plt.ion()
    fig, ax = plt.subplots()
    x, y = [], []
    i = 0

    line, = ax.plot([], [], 'b-')  # Initialize an empty plot line

    while not quit.is_set():
        try:
            # grab data from the queue
            data = queue.get(block=True, timeout=0.5)

            # Update x and y
            x.extend(range(i, i + len(data)))
            y.extend(data)
            i += len(data)

            # Keep only the most recent 100 points
            if len(x) > 100:
                x = x[-100:]
                y = y[-100:]

            # autoscale the view
            line.set_data(x, y)
            ax.relim()
            ax.autoscale_view()

            # actually plot it
            plt.draw()
            plt.pause(0.1)
        except Empty:
            plt.pause(0.1) # gracefully handle timeout

    print("plot_ui done", flush=True)
    plt.close(fig)

When I run this, it mostly does what I want. The matplotlib window appears and I can add data to it with my REPL command fake_data -n 100. The problem is that the icon for the matplotlib window keeps blinking in the taskbar (this is windows 10). This makes me think something is wrong.

Here’s the commands I issue in the REPL…

I get even more problems when I try to continuously update the queue with more data generated from yet another thread. That’s more involved to explain, so I am just posting a question about this more simplified situation to see if this is even the right path.

Is this approach correct?
Why is the icon for matplotlib blinking continuously?
Is it OK to implement a REPL as I have done in it’s own thread?

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật