0

I wrote this script where I want to create different independent figures that I can create with

Worker().start()

If it works in standalone for only 1 thread, It does not work in IPython and I cannot create multiple instances of Worker(). What is my mistake?

#!/usr/bin/env python
import pylab
import threading
import random

class Worker:
    def worker(self):
        samples = 100
        x_axis = pylab.arange(0,samples,1)
        y_axis = pylab.array([0]*samples)

        fig = pylab.figure(1)

        ax  = fig.add_subplot(111)
        ax.grid(True)
        ax.set_title("Realtime Plot")
        ax.set_xlabel("Time")
        ax.set_ylabel("Amplitude")
        ax.axis([0,samples,-1,1])

        line1 = ax.plot(x_axis,y_axis,'-')
        values = [0 for x in range(samples)]

        for i in range(100):
            values.append(random.random())
            nsamples = min(len(values),samples)
            x_axis = pylab.arange(len(values)-nsamples,len(values),1)
            line1[0].set_data(x_axis,pylab.array(values[-nsamples:]))
            ax.axis(
                    [x_axis.min(),
                     x_axis.max(),
                     min(pylab.array(values[-nsamples:])),
                     max(pylab.array(values[-nsamples:]))
                   ])
            pylab.draw()
            pylab.pause(0.01)       
        pylab.close()

    def start(self):
        self.t = threading.Thread(target=self.worker)
        self.t.start()

    def stop(self):
        self.t.stop

Here the error I get:

QPixmap: It is not safe to use pixmaps outside the GUI thread
QPixmap: It is not safe to use pixmaps outside the GUI thread
QObject::startTimer: QTimer can only be used with threads started with QThread
QObject::startTimer: QTimer can only be used with threads started with QThread
QObject::startTimer: QTimer can only be used with threads started with QThread
QObject::startTimer: QTimer can only be used with threads started with QThread
/home/canard/anaconda2/lib/python2.7/site-packages/matplotlib/backend_bases.py:2399: MatplotlibDeprecationWarning: Using default event loop until function specific to this GUI is implemented
  warnings.warn(str, mplDeprecation)
python: Fatal IO error 11 (Resource temporarily unavailable) on X server :0.

I did try on Ubuntu + Anaconda and Cygwin Python 2.7 with WxAgg.

nowox
  • 25,978
  • 39
  • 143
  • 293
  • First off, what are you trying to do? Are you trying to make a multi-threaded gui application, or are you trying to generate static figures in parallel? If it's the latter, use a non-interactive matplotlib backend. As far as what's going wrong, you're running into problems because you're creating multiple Qt gui windows that expect to share the same mainloop, but are actually in different threads. (Also, if you want to actually use WxAgg, you'll need to specify the matplotlib backend. Right now, you're using Qt.) – Joe Kington Nov 17 '15 at 17:48
  • As said, I am using `WxAgg` only on Cygwin. On Ubuntu I am using `Qt`. I have no ideas of what I am trying to do. I would like to instantiate a figure with a different content that is updating on its own multiple times. I may want to call it during a script execution that will not end before all the figures are closed or call it directly from IPython. I am still investigating on this previous [question](http://stackoverflow.com/questions/33696861/how-to-manipulate-figures-while-a-script-is-running-in-python) – nowox Nov 17 '15 at 17:55
  • 1
    Ah, the other question makes it more clear! You're wanting to run things on a timer or other callback from within the gui mainloop. Using mutliple threads will actually make things much harder, in this case. Instead, you can have matplotlib call an update function on a timer inside the gui's loop or connect a callback to a mouse event (e.g. update with clicked, etc). Alternatively, the `matplotlib.animation` module makes simple cases much easier to implement. – Joe Kington Nov 17 '15 at 17:58
  • I should take a look at `matplotlib.animation`, but first, I will need to find plenty of examples :) – nowox Nov 17 '15 at 18:06
  • All the examples of `matplotlib.animation` are not using threads. Any hint based on my example? – nowox Nov 17 '15 at 18:35
  • 2
    Well, there's no need for a separate thread. I think you're a touch confused about what threads are in Python. Threads in Python can't allow two processors to work simultaneously. Give me a bit and I'll give an example. – Joe Kington Nov 17 '15 at 18:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/95438/discussion-between-nowox-and-joe-kington). – nowox Nov 18 '15 at 10:57

0 Answers0