问题描述
在我的具有Python 3.7.7和matplotlib 3.3.1的ubuntu 18.04 VM中,此代码可以正常运行:
plt.rcParams['toolbar'] = 'toolmanager'
fig = plt.figure()
tm = fig.canvas.manager.toolmanager
tm.remove_tool('help') # Fails here in ubuntu in Azure!
但是,当从我的Azure DevOps构建管道中的单元测试中调用相同的代码时,它在tm.remove_tool('help')
Microsoft托管的VM上的ubuntu-18.04
上失败,
File "/opt/hostedtoolcache/Python/3.7.9/x64/lib/python3.7/site-packages/matplotlib/backend_managers.py",line 228,in remove_tool
tool.destroy()
AttributeError: 'nonetype' object has no attribute 'destroy'
在ubuntu-18.04
Azure VM上的Python 3.6.12和3.7.9中都会发生这种情况。两者也都使用matplotlib 3.3.1。
然而,相同的代码在Windows windows-2019
Microsoft托管的VM(Python 3.6.8和3.7.9)以及matplotlib 3.3.1中的Windows上均无错误运行。
有没有其他人看到过此消息并得到了修复或解决方法?不幸的是,我无法在自己的ubuntu VM上重现此内容。
也许Microsoft托管的ubuntu-18.04
VM缺少了matplotlib需要的东西?还是有一个奇怪的matplotlib错误?当我使用matplotlib 3.1.1时,在Azure中没有看到此问题。
2020年9月2日更新
在初始化print("Tools: %s" % tm._tools)
之后添加行tm
之后,我发现tm._tools
是一个dict,在Windows的Azure中有很多条目(而tm._tools['help']
是{{1} }对象)。但是在Azure上的Linux中,matplotlib.backends._backend_tk.HelpTk
是一个空字典:tm._tools
!
那么我需要在Linux中为matplotlib做一些额外的事情吗? here列出了Azure使用的18.04 ubuntu VM上的软件包,如果有帮助,请包括以下软件包:
- libgtk-3-0
- tk
解决方法
对于Azure为什么会不幸地这样做,我没有答案。 不过,作为解决方法,您总是可以根据需要探索在没有工具栏的情况下绘制图形的方式。
类似:
import tkinter as tk
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
data = [i for i in range(5)]
window = tk.Tk()
artist = Figure()
artist.add_subplot().plot(data)
canvas = FigureCanvasTkAgg(artist,master=window)
canvas.draw()
canvas.get_tk_widget().pack()
window.mainloop()