【问题标题】:set matplotlib backend on jupyter for plots that change over the time在 jupyter 上为随时间变化的图设置 matplotlib 后端
【发布时间】:2017-03-31 05:49:43
【问题描述】:

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

【问题讨论】:

    标签: python matplotlib jupyter-notebook


    【解决方案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_())
    

    【讨论】:

      猜你喜欢
      • 2016-08-08
      • 1970-01-01
      • 2016-06-26
      • 1970-01-01
      • 2019-05-06
      • 1970-01-01
      • 2017-03-11
      • 1970-01-01
      • 2020-05-31
      相关资源
      最近更新 更多