我制作了以下脚本,我们称之为test-matplotlib-printbackend.py
:
#!/usr/bin/env python
import matplotlib as mpl
import matplotlib.pyplot as plt
xx = range(100)
yy = [i*2+5 for i in xx]
fig, ax = plt.subplots()
ax.plot(xx, yy)
print("Matplotlib plt backend: {}".format(plt.get_backend()))
plt.show()
对于Windows 10上MINGW64下的Python 3,以及Windows 10上Anaconda下的Python3,我得到了打印输出:
Matplotlib plt backend: Qt5Agg
然而,对于Rasbian上的Python3(Raspberry Pi的Debian OS(,我得到了打印输出:
Matplotlib plt backend: TkAgg
这让我很困惑,因为我有点期待TkAgg会成为所有平台上matplotlib的默认GUI后端!?
所以,我只是想知道——不同平台的Matplotlib的默认GUI后端有记录吗?
后端选择逻辑不是很透明,也没有很好的文档记录。
在现代matplotlib中;默认后端";,即CCD_ 2被设置为"0";哨兵">
导入matplotlib后,将选择候选列表["macosx", "qt5agg", "qt4agg", "gtk3agg", "tkagg", "wxagg"]
中的第一个工作后端。
为了避免这种自动选择,您可以通过rcParams['backend']
参数或MPLBACKEND
环境变量手动设置后端。该部分记录为
默认后端在mpl.rcParams['backend']
中定义。在Linux_x64上,它被设置为'Qt5Agg'
。matplotlib.backends.__init__
中的_get_running_interactive_framework
函数检查可用的后端。CCD_ 10在优先级列表中最高。pyplot.switch_backend
函数使用_get_running_interactive_framework
在导入时选择有效使用的后端。在此处搜索线路switch_backend(rcParams["backend"])
:https://matplotlib.org/3.1.1/_modules/matplotlib/pyplot.html
您可以检查在Rasbian上是否有PyQt5
,以及rcParams条目是什么。