在 jupyter notebook中使用matplotlib.pyplot画图,如果不显示图片,比如只显示
%matplotlib inline
如果没有,请添加这一句之后再试。如果还是不行,请查看以下内容:
【环境】mac / ubuntu / windows
【解决】
1,重新安装ipywidgets, widgetsnbextension 。在终端中执行:
pip install --upgrade --force-reinstall ipywidgets
# for windows cmd: pip install --upgrade --force-reinstall ipywidgets --user
pip install --upgrade --force-reinstall widgetsnbextension
# for windows cmd: pip install --upgrade --force-reinstall widgetsnbextension --user
2,安装jupyter-js-widgets/extension,在终端中执行:
jupyter nbextension install --py widgetsnbextension
# for windows cmd: jupyter nbextension install --py widgetsnbextension --user
# 输出:
'''
Installing C:\Users\Lenovo\AppData\Roaming\Python\Python36\site-packages\widgetsnbextension\static -> jupyter-js-widgets
Making directory: C:\Users\Lenovo\AppData\Roaming\jupyter\nbextensions\jupyter-js-widgets\
Copying: C:\Users\Lenovo\AppData\Roaming\Python\Python36\site-packages\widgetsnbextension\static\extension.js -> C:\Users\Lenovo\AppData\Roaming\jupyter\nbextensions\jupyter-js-widgets\extension.js
Copying: C:\Users\Lenovo\AppData\Roaming\Python\Python36\site-packages\widgetsnbextension\static\extension.js.map -> C:\Users\Lenovo\AppData\Roaming\jupyter\nbextensions\jupyter-js-widgets\extension.js.map
- Validating: ok
To initialize this nbextension in the browser every time the notebook (or other app) loads:
jupyter nbextension enable widgetsnbextension --user --py
'''
3,在jupyter notebook中开启widgetsnbextension。在终端中执行:
jupyter nbextension enable --py widgetsnbextension --sys-prefix
# for windows cmd: jupyter nbextension enable --py widgetsnbextension --sys-prefix --user
# 输出如下:
# Enabling notebook extension jupyter-js-widgets/extension...
# - Validating: OK
4,打开jupyter notebook。在终端中执行:
jupyter notebook
5,然后可以运行有widgets内容的代码。
-------------------------------------------------------------------报错及解决-------------------------------------------------------
第3步执行时报错:
$ jupyter nbextension enable --py widgetsnbextension --sys-prefix --user
Enabling notebook extension jupyter-js-widgets/extension...
Traceback (most recent call last):
File "/home/xxx/.local/bin/jupyter-nbextension", line 8, in
sys.exit(main())
File "/home/xxx/.local/lib/python3.6/site-packages/jupyter_core/application.py", line 268, in launch_instance
return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
File "/home/xxx/.local/lib/python3.6/site-packages/traitlets/config/application.py", line 664, in launch_instance
app.start()
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/nbextensions.py", line 988, in start
super(NBExtensionApp, self).start()
File "/home/xxx/.local/lib/python3.6/site-packages/jupyter_core/application.py", line 257, in start
self.subapp.start()
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/nbextensions.py", line 896, in start
self.toggle_nbextension_python(self.extra_args[0])
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/nbextensions.py", line 872, in toggle_nbextension_python
logger=self.log)
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/nbextensions.py", line 483, in enable_nbextension_python
logger=logger)
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/nbextensions.py", line 386, in _set_nbextension_state_python
for nbext in nbexts]
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/nbextensions.py", line 386, in
for nbext in nbexts]
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/nbextensions.py", line 351, in _set_nbextension_state
cm.update(section, {"load_extensions": {require: state}})
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/config_manager.py", line 136, in update
self.set(section_name, data)
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/config_manager.py", line 110, in set
self.ensure_config_dir_exists()
File "/home/xxx/.local/lib/python3.6/site-packages/notebook/config_manager.py", line 67, in ensure_config_dir_exists
os.makedirs(self.config_dir, 0o755)
File "/usr/lib/python3.6/os.py", line 210, in makedirs
makedirs(head, mode, exist_ok)
File "/usr/lib/python3.6/os.py", line 210, in makedirs
makedirs(head, mode, exist_ok)
File "/usr/lib/python3.6/os.py", line 220, in makedirs
mkdir(name, mode)
PermissionError: [Errno 13] Permission denied: '/usr/etc'
解决:去掉 --sys-prefix 即可
$ jupyter nbextension enable --py widgetsnbextension --user
--------------------------------------------------------------------------------------------------------------------------------
【致谢】
minrk @ https://github.com/jupyter/notebook/issues/1731
landryshuai @ https://github.com/jupyter-widgets/ipywidgets/issues/1146
https://stackoverflow.com/questions/47396512/jupyter-widget-does-not-appear-in-notebook
http://cn.voidcc.com/question/p-adstwnju-ve.html
https://www.soinside.com/question/ZtKAd9hv5PCjCabNUp6mxA