Python 在jupyter上为随时间变化的绘图设置matplotlib后端

Python 在jupyter上为随时间变化的绘图设置matplotlib后端,python,matplotlib,jupyter-notebook,Python,Matplotlib,Jupyter Notebook,我想从Jupyter笔记本页面上画出我的情节。我记得“%matplotlib qt”应该是在脚本开头键入的正确命令 %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() 但它不工作,因为我得到一个内联图和以下警告 Warni

我想从Jupyter笔记本页面上画出我的情节。我记得“%matplotlib qt”应该是在脚本开头键入的正确命令

%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()
但它不工作,因为我得到一个内联图和以下警告

Warning: Cannot change to a different GUI toolkit: qt. Using notebook instead.
你能帮我了解一下我的jupyter出了什么问题吗

我想在浏览器外绘图的原因是,它看起来是在Jupyter上绘制随时间变化的绘图的唯一方法,事实上,我可以使用以下代码在spyder上轻松完成这项工作

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上提供随时间变化的绘图?

这段代码至少为您提供了一个外部窗口,您可以在其中进行操作。 摘自pythonspot.com。我肯定会有办法增加运动等,但我已经过了8个小时的就寝时间了

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_())