如何在matplotlib / Python中更改后端。
如何在matplotlib / Python中更改后端。
我正为以下问题苦苦挣扎。我需要生成一个包含一系列图表的报告。除了一个图表之外,所有这些图表都是使用Matplotlib的默认后端(TkAgg)制作的。其中一个图表需要使用Cairo后端制作,原因是我正在绘制一个igraph图,而这只能使用Cairo来绘制。\n问题在于我无法实时更改后端,例如以下操作不起作用:
\nmatplotlib.pyplot.switch_backend(\'cairo.png\')
\n(我知道switch_backend功能是实验性的)\n我还尝试过matplotlib.use(\"cairo.png\")
,但这会导致导入问题,因为matplotlib.use(\"cairo.png\")
语句应该在导入matplotlib.pyplot
之前。\n但是在脚本的生命周期中,我需要两个不同的后端。\n所以我的问题是,有人有一个代码片段可以展示如何在Matplotlib中切换后端吗?\n非常感谢!\n更新:\n我编写了一个代码片段,加载matplotlib,显示默认后端,卸载matplotlib,重新加载并更改后端:\n
import matplotlib import matplotlib.pyplot as plt import sys print matplotlib.pyplot.get_backend() modules = [] for module in sys.modules: if module.startswith('matplotlib'): modules.append(module) for module in modules: sys.modules.pop(module) import matplotlib matplotlib.use("cairo.png") import matplotlib.pyplot as plt print matplotlib.pyplot.get_backend()
\n但这真的是正确的方法吗?\n更新2:昨天我有一些严重的思维停滞...最简单和最明显的解决方案是对所有图表使用Cairo后端,而不是切换后端:)\n更新3:实际上,这仍然是一个问题,所以任何知道如何动态切换matplotlib后端的人...请发布您的答案。
问题的出现原因是使用reload函数无法重新加载matplotlib和matplotlib.pyplot模块。解决方法是使用importlib.reload函数重新加载matplotlib模块,并通过matplotlib.use函数更改后端。
import matplotlib matplotlib.use('agg') import importlib matplotlib = importlib.reload(matplotlib) matplotlib.use('cairo.png')
在IPython中可能会出现问题,但在常规控制台中可以正常工作。以下是一个示例,说明reload函数不起作用的情况:
python -c "import matplotlib.pyplot as plt; import matplotlib; from importlib import reload; matplotlib = reload(matplotlib); matplotlib.use('Agg')"
如何在matplotlib / Python中更改后端?
在使用matplotlib / Python时,有时可能遇到需要更改后端的情况。下面是一个关于如何更改后端的实验性特性示例:
import matplotlib.pyplot as plt plt.switch_backend('newbackend')
从matplotlib文档中获取。
根据文档的描述,这个功能是实验性的,只有在切换到图像后端时才会起作用。例如,如果您有一堆要从交互式ipython会话中运行的PostScript脚本,您可能希望在运行它们之前切换到PS后端,以避免弹出一堆GUI窗口。如果您尝试从一个GUI后端交互地切换到另一个GUI后端,可能会出现错误。执行此命令将关闭所有打开的窗口。
但是,有时候可能会遇到以下错误消息:"Unrecognized backend string 'newbackend'。 What should I do?"。
这时候我们需要将"newbackend"替换为一个GUI后端,例如'Qt4Agg'。
以下是解决方法的代码示例:
import matplotlib.pyplot as plt plt.switch_backend('Qt4Agg')
通过将'newbackend'替换为适当的GUI后端,可以解决这个问题。
如何在matplotlib/Python中更改后端
========================
在六年后,我遇到了一个类似的问题,当我尝试决定使用哪个后端时。
注意看下面的警告。
以下的代码片段对我来说非常有效:
import matplotlib gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg'] for gui in gui_env: try: print("testing", gui) matplotlib.use(gui, warn=False, force=True) from matplotlib import pyplot as plt break except: continue print("Using:", matplotlib.get_backend())
正如你所推测的,只需在强制使用新的后端后重新导入`matplotlib.pyplot`即可更换后端。
matplotlib.use('WXAgg', warn=False, force=True) from matplotlib import pyplot as plt print("Switched to:", matplotlib.get_backend())
对于那些仍然遇到问题的人,以下代码将打印出:
- 非GUI后端的列表;
- GUI后端的列表;
- 然后尝试使用每个GUI后端来查看它是否存在并正常工作。
import matplotlib gui_env = [i for i in matplotlib.rcsetup.interactive_bk] non_gui_backends = matplotlib.rcsetup.non_interactive_bk print("Non Gui backends are:", non_gui_backends) print("Gui backends I will test for", gui_env) for gui in gui_env: print("testing", gui) try: matplotlib.use(gui, warn=False, force=True) from matplotlib import pyplot as plt print(" ", gui, "Is Available") plt.plot([1.5, 2.0, 2.5]) fig = plt.gcf() fig.suptitle(gui) plt.show() print("Using ..... ", matplotlib.get_backend()) except: print(" ", gui, "Not found")
警告:matplotlib自3.3.0版本起的更改
- matplotlib.use的第一个参数已从arg重命名为backend(仅在通过关键字传递时相关)。
- matplotlib.use的参数warn已被删除。如果设置了force,则无法切换后端时将始终引发ImportError错误;如果需要的话,请捕获该错误。
- matplotlib.use的所有参数除了第一个参数外,现在都是关键字参数。
感谢!这对我帮助很大,帮助我找到了这个示例的合适后端:stackoverflow.com/questions/11874767/…
Rolf of Saxony,哈哈,非常有帮助,我仍然在Ubuntu和Android的帖子上收到来自近十年前的回复。
不再支持`warn=False`参数。注意了,谢谢。已添加警告信息。
必须得说声谢谢。你为我节省了大量的工作!
太好了,那么我的工作就完成了;)