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

问题描述

Jupyter环境的默认后端

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

你可能感兴趣的:(Matplotlib,matplotlib,后端,jupyter,backend)