天天看点

Jupyter Notebook环境下matplotlib后端切换异常解决方法

问题描述

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()
           

继续阅读