Use the interactive mode in an embedded Matplotlib GUI application with PyQt5
<p>I wrote a program in Python 3.5, PyQt5 and Matplotlib 1.5.3, where I used the pyplot interactive mode. Here a minimal example:</p>

<pre><code>import numpy as np
import matplotlib.pyplot as plt

plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
ax.grid(True)

for i in range (0, 10):
    plt.scatter(i, np.sin(i))
    fig.canvas.draw()

plt.ioff()
plt.show()
</code></pre>

<p>Because now I am writing the GUI and I can't use pyplot, because its own eventloop interferes with the Qt one, I was re-writing that part of the code, but I didn't find the equivalent of interactive mode into the Matplotlib widget with PyQt5. This is the basic example:</p>

<pre><code>import sys
import time

from matplotlib.figure import Figure

from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as NavigationToolbar

import numpy as np
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget

if __name__ == '__main__':

    fig = Figure()
    canvas = FigureCanvas(fig)
    ax = fig.add_subplot(111)
    ax.grid(True)

    for i in range (0, 10):
        ax.scatter(i, np.sin(i))
        fig.canvas.draw()

    ui = QWidget()

    toolbar = NavigationToolbar(canvas, ui, coordinates=True)

    vbox = QVBoxLayout()
    vbox.addWidget(canvas)
    vbox.addWidget(toolbar)
    ui.setLayout(vbox)
    ui.show()

    app = QApplication(sys.argv)
    sys.exit(app.exec_())
</code></pre>

<p>I would like, as the first example, the plot is updated at each iteration.
How can I modify the code?</p>

<p>Thanks!
Ciccio</p>