【问题标题】:jupyterlab interactive plotjupyterlab 交互式绘图
【发布时间】:2018-10-13 10:39:34
【问题描述】:

我开始使用 Jupyter notebooks 中的 Jupyterlab。在我曾经使用的笔记本中:

import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure()
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)

用于交互式绘图。现在给了我(在 jupyterlab 中):

JavaScript output is disabled in JupyterLab

我也尝试过魔法(安装了jupyter-matplotlib):

%matplotlib ipympl

但这只是返回:

FigureCanvasNbAgg()

内联图:

%matplotlib inline

工作得很好,但我想要交互式情节。

【问题讨论】:

  • 使用 Jupyter 笔记本时,我倾向于使用 > import matplotlib > matplotlib.use('nbagg') 来获得具有平移/缩放功能的交互式绘图。见:matplotlib.org/users/prev_whats_new/…
  • 然而,这是 Jupyterlab。使用这个方法也会输出JavaScript output is disabled in JupyterLab
  • 我最近遇到了同样的问题。试试这个:github.com/matplotlib/jupyter-matplotlib/issues/…
  • 对我有用的方法:1.) 使用%matplotlib widget 2.) 确保您已安装 Jupyter Lab 版本 > 1.0 和 ipywidgets > 7.5,按照此处的建议:github.com/matplotlib/jupyter-matplotlib/issues/133 3.) 打开Jupyter Lab 从终端使用命令jupyter lab。我之前使用的是一个为我提供捷径的程序,所以我不必打开终端

标签: python matplotlib jupyter-lab


【解决方案1】:

根据Georgy's suggestion,这是由于未安装 Node.js 造成的。

【讨论】:

    【解决方案2】:

    要启用 jupyter-matplotlib 后端,请使用 matplotlib Jupyter 魔法:

    %matplotlib widget
    import matplotlib.pyplot as plt
    plt.figure()
    x = [1,2,3]
    y = [4,5,6]
    plt.plot(x,y)
    

    更多信息在这里jupyter-matplotlib on GitHub

    【讨论】:

    • 我可以绘制这个图,可以按下平移缩放按钮但没有任何反应。它甚至告诉我“用鼠标左键平移”但事情发生了。如何解决?
    • 很难说,如果您在 Windows 上,请尝试sourceforge.net/projects/winpython/files 上提供的 WinPython_3.6,如上例所示。
    • 我可以通过按照github.com/matplotlib/jupyter-matplotlib 中的步骤重新安装来解决这个问题。提示:不要为此混合 conda 和 pip。
    • 是否可以在没有 plt.figure() 的情况下进行绘图。如果我们使用内联后端但不使用小部件或笔记本烘焙,这是可能的
    【解决方案3】:

    JupyterLab 3.0+

    1. 安装jupyterlabipympl

      pip 用户:

      pip install --upgrade jupyterlab ipympl
      

      conda 用户:

      conda update -c conda-forge jupyterlab ipympl
      
    2. 重启 JupyterLab。

    3. 用标题装饰包含绘图代码的单元格:

      %matplotlib widget
      
      # plotting code goes here
      

    JupyterLab 2.0

    1. 安装nodejs,例如conda install -c conda-forge nodejs.

    2. 安装ipympl,例如conda install -c conda-forge ipympl.

    3. [可选,但推荐。] 更新 JupyterLab,例如
      conda update -c conda-forge jupyterlab==2.2.9==py_0

    4. [可选,但推荐。]对于本地用户安装,运行:
      export JUPYTERLAB_DIR="$HOME/.local/share/jupyter/lab"

    5. 安装扩展:

       jupyter labextension install @jupyter-widgets/jupyterlab-manager
       jupyter labextension install jupyter-matplotlib
      
    6. 启用小部件:jupyter nbextension enable --py widgetsnbextension

    7. 重启 JupyterLab。

    8. %matplotlib widget装饰。

    【讨论】:

    • Mateen,感谢您编译步骤 - 为什么需要安装 jupyterlab-manager ?
    • 一些较旧的 jupyter lab 版本和 nodejs yarn 包(jupyter 用于其安装过程)存在问题。在这种情况下,请更新/升级您的 jupyter-lab 或遵循此修复:github.com/jupyter-widgets/ipywidgets/issues/…
    • 我在我的系统上添加了两个必要的步骤:(1) 升级 jupyterlab,(2) 启用 nbextension。
    • 我必须 conda install widgetsnbextension -c conda-forge 才能启用 widgetsnbextension(但后来我遇到了 jupyterlab 版本错误的其他问题,这是另一回事......)
    • "restart" 对我来说是关键指令。在终端中退出并重新启动 jupyterlab 是不够的,只有在重新启动计算机后,情节终于起作用了
    【解决方案4】:

    这个solution 在 jupyterlab 中工作

    import numpy as np
    import matplotlib.pyplot as plt
    from IPython.display import clear_output
    
    
    n = 10
    a = np.zeros((n, n))
    plt.figure()
    
    for i in range(n):
        plt.imshow(a)
        plt.show()
        a[i, i] = 1
        clear_output(wait=True)
    

    【讨论】:

      【解决方案5】:

      JupyterLab 3.* 的步骤

      我之前曾多次使用Mateenanswer,但是当我使用 JupyterLab 3.0.7 尝试它们时,我发现jupyter labextension install @jupyter-widgets/jupyterlab-manager 返回了一个错误并且我损坏了小部件。

      经过一番头疼和谷歌搜索后,我想我会为发现自己在这里的其他人发布解决方案。

      现在步骤已简化,我可以通过以下方式重新开始使用交互式绘图:

      1. pip install jupyterlab
      2. pip install ipympl
      3. %matplotlib widget装饰

      第 2 步将自动处理其余的依赖项,包括替换(现在已贬值?)@jupyter-widgets/jupyterlab-manager

      希望这可以节省其他人一些时间!

      【讨论】:

      • 如果您正在安装,此评论是当前正确答案。在我的机器上(Jupyterlab3、python3.9)jupyterlab-manager 实际上破坏了小部件的功能。
      【解决方案6】:

      总结

      在复杂的设置中,jupyter-lab 进程和 Jupyter/IPython 内核进程运行在不同的 Python 虚拟环境中,请注意与 Jupyter 相关的 Python 包和 Jupyter 扩展(例如ipympljupyter-matplotlib)版本以及它们在环境之间的兼容性。

      即使在单个 Python 虚拟环境中,也要确保遵守 ipympl compatibility table

      示例

      几个如何运行 JupyterLab 的示例。

      简单(st)

      我猜想,运行 JupyterLab 的最简单的跨平台方法是从 Docker 容器中运行它。您可以像这样构建和运行 JupyterLab 3 容器。

      docker run --name jupyter -it -p 8888:8888 \
        # This line on a Linux- and non-user-namespaced Docker will "share"
        # the directory between Docker host and container, and run from the user.
        -u 1000 -v $HOME/Documents/notebooks:/tmp/notebooks \
        -e HOME=/tmp/jupyter python:3.8 bash -c "
          mkdir /tmp/jupyter; \
          pip install --user 'jupyterlab < 4' 'ipympl < 0.8' pandas matplotlib; \
          /tmp/jupyter/.local/bin/jupyter lab --ip=0.0.0.0 --port 8888 \
            --no-browser --notebook-dir /tmp/notebooks;
        "
      

      当它完成时(这需要一段时间),终端中最底部的行应该是这样的。

          To access the server, open this file in a browser:
          ...
              http://127.0.0.1:8888/lab?token=abcdef...
      
      

      您只需单击该链接,JupyterLab 就会在您的浏览器中打开。关闭 JupyterLab 实例后,容器将停止。您可以使用docker start -ai jupyter 重新启动它。

      复杂

      GitHub Gist 说明了如何使用 JupyterLab 2 构建 Python 虚拟环境以及在容器中使用 Nodejs 构建所有必需的扩展,而无需在主机系统上安装 Nodejs。使用 JupyterLab 3 和 pre-build extensions,这种方法变得不那么重要了。

      上下文

      我今天在调试 %matplotlib widget 在 JupyterLab 2 中不起作用时摸不着头脑。我有单独的预构建 JupyterLab venv(如上所述),它为本地 JupyterLab 提供 Chromium“应用程序模式”(即 c.LabApp.browser = 'chromium-browser --app=%s' in配置),以及来自具有特定依赖关系(很少更改)的简单 Python venv 的一些 IPython 内核以及将自身暴露为 IPython 内核的应用程序。交互式“小部件”模式的问题以不同的方式表现出来。

      例如,拥有

      • 在 JupyterLab“主机”venv 中:jupyter-matplotlib v0.7.4 扩展和ipympl==0.6.3

      • 在内核 venv 中:ipympl==0.7.0matplotlib==3.4.2

      在浏览器控制台中出现以下错误:

      • Error: Module jupyter-matplotlib, semver range ^0.9.0 is not registered as a widget module
      • Error: Could not create a model.
      • Could not instantiate widget

      在 JupyterLab 用户界面中:

      • %matplotlib widget 重启成功
      • 图表卡在“正在加载小部件...”中
      • 使用图表输出重新运行单元格时没有任何内容
      • 在之前的尝试中,%matplotlib widget 可能会引发类似 KeyError: '97acd0c8fb504a2288834b349003b4ae' 的问题

      在浏览器控制台的内核 venv 中降级 ipympl==0.6.3

      • Could not instantiate widget
      • Exception opening new comm
      • Error: Could not create a model.
      • Module jupyter-matplotlib, semver range ^0.8.3 is not registered as a widget module

      一旦我根据ipympl compatibility table 制作了包/扩展:

      • 在 JupyterLab“主机”venv 中:jupyter-matplotlib v0.8.3 扩展,ipympl==0.6.3

      • 在内核 venv 中:ipympl==0.6.3, matplotlib==3.3.4

      它或多或少按预期工作。好吧,除了我将%matplotlib widget 每个单元格与图表放在一起之外,还有很多小故障,比如说在重新启动时,第一个图表“累积”了笔记本中所有图表的所有内容。对于每个单元格%matplotlib widget,一次只有一个图表处于“活动状态”。并且在重新启动时仅呈现最后一个小部件(但手动重新运行单元格会进行修复)。

      【讨论】:

        猜你喜欢
        • 2016-05-08
        • 2020-12-02
        • 2021-06-16
        • 2012-01-05
        • 2018-07-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-14
        相关资源
        最近更新 更多