jupyter lab安装ipympl提供matplotlib交互式后端
概述
在Jupyter Notebook或Jupyter Lab环境中运行以下代码可知
import matplotlib
print(matplotlib.get_backend())
Jupyter环境的默认后端为module://ipykernel.pylab.backend_inline。
默认情况下,使用默认后端,matplotlib在Jupyter环境即便开启了交互绘图模式,也不能正常实现交互式绘图。
如果在Jupyter环境中实现matplotlib交互式绘图,必须使用Matplotlib Jupyter Widget Backend,即ipympl包(原名:jupyter-matplotlib)。
ipympl包是matplotlib开发组专门为Jupyter环境开发的交互式后端,利用Jupyter Widget 实现功能,项目地址为 https://github.com/matplotlib/ipympl
使用ipympl
安装
使用pip或conda进行安装。
pip install ipympl
基本使用
安装完ipympl,切记要重启Jupyter Notebook或Jupyter Lab服务。
在jupyter中执行魔术命令%matplotlib widget将后端切换为ipympl。
%matplotlib widget
import matplotlib
matplotlib.ge