Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python jupyterlab交互绘图_Python_Matplotlib_Jupyter Lab - Fatal编程技术网

Python jupyterlab交互绘图

Python jupyterlab交互绘图,python,matplotlib,jupyter-lab,Python,Matplotlib,Jupyter Lab,我开始使用Jupyter笔记本上的Jupyter实验室。在我以前使用的笔记本中: import matplotlib.pyplot as plt %matplotlib notebook plt.figure() x = [1,2,3] y = [4,5,6] plt.plot(x,y) 用于交互式绘图。现在我(在jupyterlab中): 我还尝试了魔术(已安装): 但这只是返回: FigureCanvasNbAgg() 内联图: %matplotlib inline 工作正常,但我需要

我开始使用Jupyter笔记本上的Jupyter实验室。在我以前使用的笔记本中:

import matplotlib.pyplot as plt
%matplotlib notebook
plt.figure()
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)
用于交互式绘图。现在我(在jupyterlab中):

我还尝试了魔术(已安装):

但这只是返回:

FigureCanvasNbAgg()
内联图:

%matplotlib inline

工作正常,但我需要交互式绘图。

根据,这是由于没有安装Node.js造成的

要启用jupyter matplotlib后端,请使用matplotlib jupyter magic:

%matplotlib widget
import matplotlib.pyplot as plt
plt.figure()
x = [1,2,3]
y = [4,5,6]
plt.plot(x,y)
更多信息请点击这里

完成步骤
  • 安装
    nodejs
    ,例如
    conda安装nodejs
  • 安装
    ipympl
    ,例如
    pip安装ipympl
  • [可选,但推荐;更新JupyterLab,例如
    pip安装--升级jupyterlab
    ]
  • [可选,但推荐;对于本地用户安装,请运行:
    export-JUPYTERLAB\u DIR=“$HOME/.local/share/jupyter/lab”
    ]
  • 安装扩展:

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

  • 重新启动JupyterLab
  • %matplotlib小部件装饰

  • 不推荐,但要盲目地让小部件扩展在Anaconda中工作,可以在终端窗口中运行以下操作:

    conda install -y nodejs
    pip install ipympl
    pip install --upgrade jupyterlab
    jupyter labextension install @jupyter-widgets/jupyterlab-manager
    jupyter labextension install jupyter-matplotlib
    jupyter nbextension enable --py widgetsnbextension
    
    这在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)
    
    JupyterLab 3的步骤* 我以前曾多次使用过的,但当我在JupyterLab 3.0.7中尝试它们时,我发现
    jupyter labextension install@jupyter widgets/JupyterLab manager
    返回了一个错误,我破坏了小部件

    在经历了大量的头痛和谷歌搜索之后,我想我会把这个解决方案发布给任何在这里找到自己的人

    这些步骤现在已经简化,我可以继续使用以下交互式绘图:

  • pip安装jupyterlab
  • pip安装ipympl
  • %matplotlib小部件装饰
  • 步骤2将自动处理其余的依赖项,包括(现在已折旧?)的替换项
    @jupyter widgets/jupyterlab manager

    希望这能为其他人节省一些时间

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

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

    例子 下面是几个如何运行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;
      "
    
    您只需单击该链接,JupyterLab就会在浏览器中打开。关闭JupyterLab实例后,容器将停止。您可以使用
    docker start-ai jupyter
    重新启动它

    复杂的 这说明了如何使用JupyterLab 2构建Python虚拟环境,以及如何在容器中使用NodeJ构建所有必需的扩展,而无需在主机系统上安装NodeJ。使用JupyterLab 3,这种方法就不那么重要了

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

    比如说,

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

    • 在内核版本中:
      ipympl==0.7.0
      matplotlib==3.4.2

    在浏览器控制台中,我遇到以下错误:

    • 错误:模块jupyter matplotlib,semver range ^0.9.0未注册为小部件模块
    • 错误:无法创建模型。
    • 无法实例化小部件
    在JupyterLab UI中:

    • %matplotlib小部件重新启动时成功
    • 图表卡在“加载小部件…”
    • 重新运行具有图表输出的单元格时无任何内容
    • 在以前的尝试中,
      %matplotlib小部件
      可能会引发类似于
      KeyError的问题:“97acd0c8fb504a2288834b349003b4ae”
    在浏览器控制台的内核venv中降级
    ipympl==0.6.3

    • 无法实例化小部件
    • 异常打开新通信
    • 错误:无法创建模型。
    • 模块jupyter matplotlib,semver range ^0.8.3未注册为小部件模块
    一旦我根据以下内容制作了包/扩展:

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

    • 在内核版本中:
      ipympl==0.6.3
      matplotlib==3.3.4


    它或多或少像预期的那样工作。嗯,确实存在一些小问题,比如,除了我将
    %matplotlib widget
    与图表放在每个单元格中,比如重新启动时,第一个图表“累积”了笔记本中所有图表的所有内容。使用每个单元格的
    %matplotlib小部件
    ,一次只有一个图表处于“活动”状态。重新启动时,只渲染最后一个小部件(但手动重新运行单元格会进行修正)。

    使用Jupyter笔记本电脑时,我倾向于使用>导入matplotlib>matplotlib。使用('nbagg')获得具有平移/缩放功能的交互式绘图。硒
    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)
    
    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...