Set matplotlib backend to jupyter for sites that change over time

I would like to extract my stories from the Jupyter laptop page. As I recall, "% matplotlib qt" should be the right command to enter at the beginning of the script

%matplotlib qt
import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-3, 3, 0.01)
y = np.sin(np.pi*x)/(np.pi*x)
plt.figure() 
plt.plot(x, y)
plt.show()

But this does not work, as I get a built-in graph and the following warning

Warning: Cannot change to a different GUI toolkit: qt. Using notebook instead.

Could you help me understand what happened to my jupiter?

The reason I would like to build outside the browser is that it looks like the only way to have a plot that changes over time on Jupyter, in fact I can easily do this job on spyder with the following code

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-3, 3, 0.01)
fig = plt.figure()
ax = fig.add_subplot(111)
for j in range(1, 10):
    y = np.sin(np.pi*x*j)/(np.pi*x*j)
    line, = ax.plot(x, y, 'b')
    plt.pause(0.3)
    plt.draw()
    line.remove()

Jupyter. , Jupyter?

+4
1

, , , . pythonspot.com. , .., 8 . Zzzzz

import sys

from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QPushButton
from PyQt5.QtGui import QIcon


from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np

import random

class App(QMainWindow):

    def __init__(self):
        super().__init__()
        self.left = 10
        self.top = 10
        self.title = 'PyQt5 matplotlib example - pythonspot.com'
        self.width = 640
        self.height = 400
        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        m = PlotCanvas(self, width=5, height=4)
        m.move(0,0)
        self.show()


class PlotCanvas(FigureCanvas):

    def __init__(self, parent=None, width=5, height=4, dpi=100):
        fig = Figure(figsize=(width, height), dpi=dpi)
        self.axes = fig.add_subplot(111)

        FigureCanvas.__init__(self, fig)
        self.setParent(parent)

        FigureCanvas.setSizePolicy(self,
                QSizePolicy.Expanding,
                QSizePolicy.Expanding)
        FigureCanvas.updateGeometry(self)
        self.plot()


    def plot(self):

        x = np.arange(-3, 3, 0.01)
        ax = self.figure.add_subplot(111)
        ax.set_title('PyQt Matplotlib Example')
        for j in range(1, 10):
            y = np.sin(np.pi*x*j)/(np.pi*x*j)
            line, = ax.plot(x, y, 'b')
            self.draw()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
0

Source: https://habr.com/ru/post/1660995/


All Articles