0

I wish to show graph as a slideshow by reading data from the file. First I wish to plot first set of data,then next and so on. What I have tried is:

class MatplotlibWidget(QMainWindow):

    def __init__(self):
        ---
        self.playbutton.clicked.connect(self.drawGraph)
        self.pausebutton.clicked.connect(self.pauseGraph)

        ----      

   def drawGraph(self):
        f1 = open('TESTIP2.txt', 'r')        
        data = np.genfromtxt(f1)
        m = np.size(data, 0)
        n = np.size(data, 1)
        x = data[:, 0].reshape(m, 1)
        y = data[:, 1].reshape(m, 1)
        iters = m // 4
        current_iter=0
        self.plotGraph(x,y,iters,current_iter)

   def plotGraph(x,y,iters,current_iter):
        for i in range(iters):
           self.plotDraw(x[current_iter:current_iter+iters],y[current_iter:current_iter+iters])
           current_iter=current_iter+iters
           time.sleep(1)

   def plotDraw(x,y)       
        self.MplWidget.canvas.axes.clear()
        self.MplWidget.canvas.axes.plot(x,y)
        self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'), loc='upper right')
        self.MplWidget.canvas.axes.set_title('Signal' )
        self.MplWidget.canvas.draw()

plotDraw function is called inside the loop to show each set of data, but its shows only the last set of data. Is there any way to show first, second, and so on after a specific time interval.

5
  • 3
    Can you please provide the minimal verifiable example? (stackoverflow.com/help/mcve). Commented Mar 11, 2019 at 10:38
  • Have you tried including plt.pause(1) in the for loop? Commented Mar 11, 2019 at 10:57
  • @Jack.Yes I have tried plt.pause(1), but it is not working. Commented Mar 12, 2019 at 3:38
  • @eyllanesc .Thank You so much for the help. Commented Mar 12, 2019 at 6:11
  • @eyllanesc. How to pause the running loop with a button or with a mouse hover or click and should resume if anothe rclick occurs Commented Mar 12, 2019 at 6:18

1 Answer 1

1

The easiest way is to use the QTimer from PyQt5. This is really easy to use: you specify a function that should be triggered after timeout, and you specifiy the time interval. With the following code I plot random data every second in a Matplotlib Widget inside PyQt5.

from PyQt5.QtWidgets import QMainWindow, QApplication
from PyQt5.QtCore import QTimer
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import numpy as np


class M(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setGeometry(100,100,640,480)
        self.Figure = Figure()
        self.Canvas = FigureCanvas(self.Figure)
        self.Canvas.setParent(self)
        self.Canvas.move(0,0)

        self.ax = self.Figure.add_subplot(111)
        self.plotItem, = self.ax.plot([], [])
        self.plot()

        # Create timer
        self.t = QTimer()
        self.t.timeout.connect(self.plot) # specify function 
        self.t.start(1000) # 1 s


    def plot(self):
        """plots random data and adjusts the x and y limits"""
        x = np.linspace(0, np.random.randn()*100)
        y = np.random.randn(50)

        self.plotItem.set_xdata(x)
        self.plotItem.set_ydata(y)
        self.ax.set_ylim([y.min()-1, y.max()+1])
        self.ax.set_xlim([x.min()-1, x.max()+1])
        self.Canvas.draw() # update plot


if __name__ == '__main__':
    app = QApplication([])
    m = M()
    m.show()
    app.exec_()

The above code gives you this:

Changing x and y data every second using QTimer object

you can for example use a button to trigger self.t.stop() to stop the update / cycling, and if you want to proceed you can again self.t.start(your_interval).

Sign up to request clarification or add additional context in comments.

4 Comments

Can you help me of how to pause a graph and forward and backward play for the graph.
you need to connect a button with a function that handles the timer (e.g. checks if the timer is active using the QTimer.IsActive() method. If it is active, then you would stop the timer (see my answer) or if it is paused, start the timer (again, see my answer) with your desired interval. If you plot data as a list of lists/arrays, then simply iterate over your list forward (increment the counter) or backward (decrement the counter).
Well, without seeing any of your code it is hard to tell why it is not working for you.
The code you posted in your question? That is not adapted to my answer, and it is not a fully minimal working example (see link of my last comment). Please try and provide all the necessary information that one can help. We are not here to do the work of s.o. else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.