以下程序无法在非GUI环境下工作。
我希望让这个程序在调用
我知道可以使用
因此,我考虑将Matplotlib的后端更改为自己的后端,以更改
是否有任何解释后端接口的文档?(还是其他绕过更改
plt.show
时将图形保存到一个临时的png文件中。import matplotlib.pyplot as plt
plt.scatter(2,3)
plt.scatter(4,5)
plt.show()
我知道可以使用
plt.savefig
代替plt.show
来解决这个问题(参见Save plot to image file instead of displaying it using Matplotlib)。但我不想改变程序本身。该程序可能由其他熟悉在GUI环境中使用Matplotlib的用户提供。因此,我考虑将Matplotlib的后端更改为自己的后端,以更改
show
的行为。这可以通过更改matplotlibrc
来完成。
但是关于后端的文档只解释了如何选择“内置”后端:https://matplotlib.org/faq/usage_faq.html?highlight=backend#coding-styles
文档说,后端可以指定为module://my_backend
,但它没有定义my_backend
的“接口”(应该在哪些名称中实现哪些类型的类/对象?)是否有任何解释后端接口的文档?(还是其他绕过更改
show
行为的方法?)
plt.plot([1,2,3],[2,1,4]); plt.show()
,但这个不行:fig = plt.figure(); fig.gca().plot([1,2,3],[3,3,1]); fig.show()
。我得到了以下警告信息:UserWarning: Matplotlib is currently using module://pyptex, which is a non-GUI backend, so cannot show the figure.
这显然来自于FigureManagerBase.show()
。有没有快速修复的方法? - Sébastien Loisel