Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9279537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:39:31+00:00 2026-06-18T17:39:31+00:00

First, I am new to Python. I am a long-time MatLab user (engineer, not

  • 0

First, I am new to Python. I am a long-time MatLab user (engineer, not computer scientist) and I am beginning the process of attempting to work Python, NumPy, SciPy, etc. into my workflow. So, please excuse my obvious ignorance of what is a wonderful programming language!

As my first endeavor, I decided to build an application to interact with a sensor that I am developing. The sensor has microsecond resolution (data from 512 high and 512 low energy “pixels” every 500 microseconds), but the I/O will be blocking. Since I will continually poll the device, I know threading will be important to keep the GUI responsive (the GUI will ultimately also integrate serial communication with another device, and have an image processing subroutine that operates on the sensor data). I created a threaded instance of MatPlotLib to plot these “real-time” data from the sensor. Though I’ve built the module that communicates with the sensor independently and verified I know how to do that in Python, I am starting here simply with a “simulation” of the data by generating 512 random numbers between 8 and 12 for the low energy “pixels”, and 512 random numbers between 90 and 110 for the high energy “pixels”. That is what is threaded. Working from many examples here, I also learned to use blitting to get a fast enough screen update with MatPlotLib — BUT, the problem is that unless I use put the threaded process to sleep for 20ms using time.sleep(0.02), the GUI is unresponsive. This can be verified because the interactive X,Y data point feedback from MatPlotLib doesn’t work and the ‘STOP’ button cannot be used to interrupt the process. Anything longer than time.sleep(0.02) makes the GUI operated even smoother, but at the expense of “data rate”. Anything slower than time.sleep(0.02) makes the GUI unresponsive. I’m not sure I understand why. I was going to go off and try to use GUIqwt instead, but thought I would ask here before abandoning MatPlotLib since I’m not sure that is even the problem. I am concerned that putting the thread to sleep for 20ms will mean that I miss at least 40 potential lines of data from the sensor array (40 lines * 500us/line = 20ms).

Here is the current code:

import time, random, sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class ApplicationWindow(QMainWindow):

    def __init__(self, parent = None):

        QMainWindow.__init__(self, parent)

        self.thread = Worker()

        self.create_main_frame()
        self.create_status_bar()

        self.connect(self.thread, SIGNAL("finished()"), self.update_UI)
        self.connect(self.thread, SIGNAL("terminated()"), self.update_UI)       
        self.connect(self.startButton, SIGNAL("clicked()"), self.start_acquisition)       
        self.connect(self.stopButton, SIGNAL("clicked()"), self.stop_acquisition)
        self.thread.pixel_list.connect(self.update_figure)

    def create_main_frame(self):
        self.main_frame = QWidget()

        self.dpi = 100
        self.width = 10
        self.height = 8
        self.fig = Figure(figsize=(self.width, self.height), dpi=self.dpi)
        self.axes = self.fig.add_subplot(111)               
        self.axes.axis((0,512,0,120))

        self.canvas = FigureCanvas(self.fig)
        self.canvas.setParent(self.main_frame)
        self.canvas.updateGeometry()    
        self.canvas.draw()
        self.background = None

        self.lE_line, = self.axes.plot(range(512), [0 for i in xrange(512)], animated=True)
        self.hE_line, = self.axes.plot(range(512), [0 for i in xrange(512)], animated=True)          

        self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)

        self.startButton = QPushButton(self.tr("&Start"))
        self.stopButton = QPushButton(self.tr("&Stop"))

        layout = QGridLayout()
        layout.addWidget(self.canvas, 0, 0)
        layout.addWidget(self.mpl_toolbar, 1, 0)
        layout.addWidget(self.startButton, 2, 0)       
        layout.addWidget(self.stopButton, 2, 1)

        self.main_frame.setLayout(layout)
        self.setCentralWidget(self.main_frame)

        self.setWindowTitle(self.tr("XRTdev Interface"))

    def create_status_bar(self):
        self.status_text = QLabel("I am a status bar.  I need a status to show!")
        self.statusBar().addWidget(self.status_text, 1)

    def start_acquisition(self):
        self.thread.exiting = False
        self.startButton.setEnabled(False)
        self.stopButton.setEnabled(True)
        self.thread.render()

    def stop_acquisition(self):
        self.thread.exiting = True
        self.startButton.setEnabled(True)
        self.stopButton.setEnabled(False)
        self.cleanup_UI()

    def update_figure(self, lE, hE):
        if self.background == None:
            self.background = self.canvas.copy_from_bbox(self.axes.bbox)
        self.canvas.restore_region(self.background)
        self.lE_line.set_ydata(lE)
        self.hE_line.set_ydata(hE)
        self.axes.draw_artist(self.lE_line)
        self.axes.draw_artist(self.hE_line)
        self.canvas.blit(self.axes.bbox)

    def update_UI(self):
        self.startButton.setEnabled(True)
        self.stopButton.setEnabled(False)
        self.cleanup_UI()        

    def cleanup_UI(self):
        self.background = None
        self.axes.clear()        
        self.canvas.draw()

class Worker(QThread):

    pixel_list = pyqtSignal(list, list)

    def __init__(self, parent = None):
        QThread.__init__(self, parent)
        self.exiting = False

    def __del__(self):
        self.exiting = True
        self.wait()

    def render(self):
        self.start()

    def run(self):
        # simulate I/O
        n = random.randrange(100,200)
        while not self.exiting and n > 0:
            lE = [random.randrange(5,16) for i in xrange(512)]
            hE = [random.randrange(80,121) for i in xrange(512)]
            self.pixel_list.emit(lE, hE)
            time.sleep(0.02)
            n -= 1

def main():
    app = QApplication(sys.argv)
    form = ApplicationWindow()
    form.show()
    app.exec_()

if __name__ == "__main__":
    main()

Perhaps my problem isn’t even with MatPlotLib or PyQT4, but the way I implemented threading. As I noted, I am new to this and learning. And, I’m not even sure GUIqwt will address any of these issues — but I do know I’ve seen many recommendations here to use something faster than MatPlotLib for “real time” plotting in a GUI. Thanks for the help on this!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T17:39:33+00:00Added an answer on June 18, 2026 at 5:39 pm

    [edited because QThread is confusing/confused]

    There seems to be two ways to use it, either sub-classing it (as your example and the documentation says) or creating a worker object and then moving it to a thread (See this blog post). I then get more confused when you mix signal/slots in. As Avaris says, this change may not be your problem.

    I re-worked your Worker class as a a sub-class of QObject (because this is the style I understand).

    A problem is that if you do not put a sleep in your fake data system, then you generate all the call backs to the main window in < 1s. The main thread is then essentially blocked as it clears out the signal queue. If you set the delay to your specified delay, (0.0005s), then it cranks through generating the data far faster than it can be displayed, which seems to suggest that this may not be suitable for your problem (being pragmatic, you also can’t see that fast, and it seems to do ok at 30 – 40 fps).

    import time, random, sys
    #from PySide.QtCore import *
    #from PySide.QtGui import *
    
    from PyQt4 import QtCore
    from PyQt4 import QtGui
    
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    
    class ApplicationWindow(QtGui.QMainWindow):
        get_data = QtCore.pyqtSignal()
    
        def __init__(self, parent = None):
    
            QtGui.QMainWindow.__init__(self, parent)
    
    
            self.thread = QtCore.QThread(parent=self)
            self.worker = Worker(parent=None)
            self.worker.moveToThread(self.thread)
    
            self.create_main_frame()
            self.create_status_bar()
    
            self.startButton.clicked.connect(self.start_acquisition) 
            self.stopButton.clicked.connect(self.stop_acquisition)
            self.worker.pixel_list.connect(self.update_figure)
            self.worker.done.connect(self.update_UI)
    
            self.get_data.connect(self.worker.get_data)
    
    
            self.thread.start()
    
    
        def create_main_frame(self):
            self.main_frame = QtGui.QWidget()
    
            self.dpi = 100
            self.width = 10
            self.height = 8
            self.fig = Figure(figsize=(self.width, self.height), dpi=self.dpi)
            self.axes = self.fig.add_subplot(111)               
            self.axes.axis((0,512,0,120))
    
            self.canvas = FigureCanvas(self.fig)
            self.canvas.setParent(self.main_frame)
            self.canvas.updateGeometry()    
            self.canvas.draw()
            self.background = None
    
            self.lE_line, = self.axes.plot(range(512), [0 for i in xrange(512)], animated=True)
            self.hE_line, = self.axes.plot(range(512), [0 for i in xrange(512)], animated=True)          
    
            self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame)
    
            self.startButton = QtGui.QPushButton(self.tr("&Start"))
            self.stopButton = QtGui.QPushButton(self.tr("&Stop"))
    
            layout = QtGui.QGridLayout()
            layout.addWidget(self.canvas, 0, 0)
            layout.addWidget(self.mpl_toolbar, 1, 0)
            layout.addWidget(self.startButton, 2, 0)       
            layout.addWidget(self.stopButton, 2, 1)
    
            self.main_frame.setLayout(layout)
            self.setCentralWidget(self.main_frame)
    
            self.setWindowTitle(self.tr("XRTdev Interface"))
    
        def create_status_bar(self):
            self.status_text = QtGui.QLabel("I am a status bar.  I need a status to show!")
            self.statusBar().addWidget(self.status_text, 1)
    
        def start_acquisition(self):
            self.worker.exiting = False
            self.startButton.setEnabled(False)
            self.stopButton.setEnabled(True)
            self.get_data.emit()
    
        def stop_acquisition(self):
            self.worker.exiting = True
            self.startButton.setEnabled(True)
            self.stopButton.setEnabled(False)
            self.cleanup_UI()
    
        def update_figure(self, lE, hE):
            if self.background == None:
                self.background = self.canvas.copy_from_bbox(self.axes.bbox)
            self.canvas.restore_region(self.background)
            self.lE_line.set_ydata(lE)
            self.hE_line.set_ydata(hE)
            self.axes.draw_artist(self.lE_line)
            self.axes.draw_artist(self.hE_line)
            self.canvas.blit(self.axes.bbox)
    
        def update_UI(self):
            self.startButton.setEnabled(True)
            self.stopButton.setEnabled(False)
            self.cleanup_UI()        
    
        def cleanup_UI(self):
            self.background = None
            self.axes.clear()        
            self.canvas.draw()
    
    class Worker(QtCore.QObject):
    
        pixel_list = QtCore.pyqtSignal(list, list)
        done = QtCore.pyqtSignal()
    
        def __init__(self, parent = None):
            QtCore.QObject.__init__(self, parent)
            self.exiting = True
    
        @QtCore.pyqtSlot()
        def get_data(self):
            # simulate I/O
            print 'data_start'
            n = random.randrange(100,200)
            while not self.exiting and n > 0:
                lE = [random.randrange(5,16) for i in xrange(512)]
                hE = [random.randrange(80,121) for i in xrange(512)]
                self.pixel_list.emit(lE, hE)
                time.sleep(0.05)
                n -= 1
            print 'n: ', n
            self.done.emit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Long time reader, first time poster. I am very new to python and I
First, I'm not a python programmer. I'm an old C dog that's learned new
I'm fairly new to both Django and Python. This is my first time using
I am new to Python and this is my first time asking a stackOverflow
First of all, I am new to python/nltk so my apologies if the question
I am new to programming and am learning Python as my first language. I
I have long programming background, but am new to Python, and am playing around
First of all, I'm new to python, reportlab, xhtml2pdf. I've already done my first
I'm a long time reader first time poster, glad to start participating in this
New to python (very cool), first question. I am reading a 50+ mb ascii

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.