天天看點

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

繼續閱讀