0

I simply want to put a pause clause to my plot where I can pause the real time stream voluntarily. This is what I am trying. This is an edited code. It s making two subplots: scatter and pie chart. I am able to pause the scatter plot but when I pause pie chart disappears.

def onClick(event):
    global pause
    pause ^= True
fig = plt.figure()
fig.canvas.mpl_connect('button_press_event', onClick)
plt.show()

while t<ran2:       
      if not pause:
         ax = fig.add_subplot(2,1,1)
         ay = fig.add_subplot(2,1,2)
         mdfmt = md.DateFormatter('%H:%M')
         ax.xaxis.set_major_formatter(mdfmt)
         cmap=cm.jet
         g=s[t:t+4]
         h=[]
         for i in g:
             if i == 'Match':
                 h.append(1)
             else:
                 h.append(0)
         ax.scatter(x[t:t+4],y[t:t+4],c=h,s=150,marker='<',edgecolor='None', cmap = cm.jet)
         ax.set_xlabel('Time')
         ax.set_ylabel('Action')
         mindate = min(data_mat['Date'].ix[t:t+4])
         ax.set_title('Alarm System for site SPFD02 on date %s'%mindate)
         yt=[-1,0,1]
         ax.set_yticks(yt)
         ax.set_yticklabels(('Closed','Not Ack','Assigned'))
         ax.set_xlim(x[t],x[t+4])    

# for pie chart
         l = 'Match','No-Match'
         colors = ['red','blue']
         j = s[t:t+4].count('Match')
         z = z + j
         k = s[t:t+4].count('No-Match')
         r = r + k
         sizes = [z,r]
         ay.pie(sizes,labels = l, colors = colors, autopct='%1.1f%%', shadow=True, startangle=140)
         ay.set_aspect('equal')        
         t=t+1
   else:
        print 'paused'
plt.pause(0.1)
plt.cla() 
nezz
  • 11
  • 4
  • [This](http://stackoverflow.com/questions/11874767/real-time-plotting-in-while-loop-with-matplotlib) SO question might assist you. The accepted answer has something about pausing, which you could probably use in your own code with a bit of tweaking. – kirkpatt Jun 29 '16 at 19:49
  • @kirkpatt: I am able to give a pause to my real time stream but I want to pause externally as in by clicking mouse or using keyboard. – nezz Jun 29 '16 at 19:53

1 Answers1

0

There are more elegant methods of achieving this (using matplotlib.animation, see e.g. this: https://stackoverflow.com/a/16733373/3581217 answer), but within the spirit of the example that you provided, this functionally works:

import matplotlib.pylab as pl

pause = False

def onClick(event):
    global pause
    pause ^= True

fig = pl.figure()
fig.canvas.mpl_connect('button_press_event', onClick)

nt = 100
t = 0
while t < nt:
    if not pause:
        # do stuff
        print("update plot!")
        t += 1
    else:
        print("paused")
    pl.pause(0.1)

The trick is to make pause a global variable (you were setting it in your onClick function, but also inside the loop...?), the onClick function toggles (^=) pause between True/False, and based on the state of pause you either "do stuff" or not.

But again, it might make more sense to look into matplotlib.animation...

EDIT based on comments below I don't see where your updated code goes wrong; this minimal example updates both the scatter and pie charts without a problem.

import matplotlib.pylab as plt
import numpy as np

pause = False

def onClick(event):
    global pause
    pause ^= True

fig = plt.figure()
fig.canvas.mpl_connect('button_press_event', onClick)

nt = 100
x = np.random.random(nt+4)
y = np.random.random(nt+4)
s = np.random.random(nt+4)

t = 0
while t < nt:
    if not pause:
        ax = fig.add_subplot(2,1,1)
        ay = fig.add_subplot(2,1,2)

        ax.scatter(x[t:t+4], y[t:t+4])
        ay.pie(s[t:t+4])
        t += 1
    plt.pause(0.1)
Community
  • 1
  • 1
Bart
  • 9,825
  • 5
  • 47
  • 73
  • Hi @bart : Now, I am able to pause the plots, but the problem I am facing is that my scatter plot gets paused and shows the still figure but the pie chart (another subplot) vanishes. How to overcome this issue. Thanks – nezz Jun 30 '16 at 14:22
  • You will have to provide us with more info / code, otherwise we would have to make very wild guesses on what *might* go wrong. – Bart Jun 30 '16 at 15:22
  • Hi @bart I edited my post.. Please look into it. I am making two subplots: scatter plot and pie chart. Now I am able to pause the plot but pie chart disappears when I pause and it appears again when I reclick to start the plot. Thanks – nezz Jun 30 '16 at 15:49
  • I don't see why your code won't work; my reduced version of it (I added it to my answer) works without a problem. So if you need more help, please reproduce the problem with a minimal example, otherwise we really can't help. – Bart Jun 30 '16 at 16:00
  • Thanks a lot @Bart ! I made a change of ay.cla() instead of plt.cla(). Now it is working fine! – nezz Jun 30 '16 at 21:27