问题描述
Jupyter环境的默认后端
在Jupyter Notebook或Jupyter Lab环境中运行以下代码可知
import matplotlib
print(matplotlib.get_backend())
Jupyter环境的默认后端为:
module://ipykernel.pylab.backend_inline
切换后端时出现异常
本机原后端为qt5agg。
运行matplotlib.use('tkagg')后,抛出异常ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'qt5' is currently running。
问题解决
重启jupyter内核。
先使用matplotlib.use('tkagg')切换后端,然后再导入pyplot模块。
import matplotlib
matplotlib.use('tkagg')
import matplotlib.pyplot as plt
plt.plot(1)
plt.show()
本文介绍了Jupyter Notebook或JupyterLab中默认的matplotlib后端为module://ipykernel.pylab.backend_inline,并展示了在尝试切换到'tkagg'后端时遇到的ImportError。解决方法是重启Jupyter内核,先使用matplotlib.use('tkagg')切换后端,再导入pyplot模块。
1704





