如何在matplotlib/Python中更改后端 [英] How to change backends in matplotlib / Python

查看:125
本文介绍了如何在matplotlib/Python中更改后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决以下问题.我需要生成包含图表集合的报告.除一个图表外,所有这些图表都是使用Matplotlib默认后端(TkAgg)制作的.需要使用Cairo后端制作一张图表,原因是我正在绘制igraph图形,并且只能使用Cairo进行绘制.

I am struggling with the following issue. I need to generate reports that consists of a collection of charts. All these charts, except one, are made using Matplotlib default backend (TkAgg). One chart needs to be made using the Cairo backend, the reason is that I am plotting an igraph graph and that can only be plotted using Cairo.

问题是我无法即时更改后端,例如以下操作不起作用:
matplotlib.pyplot.switch_backend('cairo.png') (我知道switch_backend功能是实验性的)

The issue is that I cannot change backends on the fly, for example the following does not work:
matplotlib.pyplot.switch_backend('cairo.png') (I know that the switch_backend functionality is experimental)

并且我也尝试过matplotlib.use("cairo.png"),但这会导致导入问题,因为matplotlib.use("cairo.png")语句应该在导入matplotlib.pyplot之前出现. 但是在脚本的整个生命周期中,我需要两个不同的后端.

and I have also tried matplotlib.use("cairo.png") but this leads to import problems as the matplotlib.use("cairo.png") statement should come before importing matplotlib.pyplot. but I need two different backends over the course of the life of the script.

所以我的问题是有人是否有代码片段显示如何在Matplotlib中切换后端?

So my question is does someone have a code snippet that shows how to switch the backend in Matplotlib?

非常感谢!

更新: 我编写了一个片段,该片段加载matplotlib,显示默认后端,卸载matplotlib,重新加载并更改后端:

UPDATE: I have written a snippet that loads matplotlib, shows the default backend, unloads matplotlib, reloads it and changes the backend:

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

但这真的是这样做的方法吗?

but is this really the way to do it?

更新2:昨天我有一些严重的大脑冻结……最简单,最明显的解决方案是对所有图表使用开罗后端,而根本不切换后端:)

UPDATE 2: I had some serious brain freeze yesterday... The simple and most obvious solution is to use the Cairo backend for all charts and not to switch the backend at all :)

更新3:实际上,这仍然是一个问题,因此任何知道如何动态切换matplotlib后端的人..请发表您的答案.

UPDATE 3: Actually, it's still an issue so anybody who knows how to dynamically switch matplotlib backends....please post your answer.

推荐答案

六年后,当我试图确定可以使用哪个backend时,我遇到了类似的问题.
此代码段对我来说效果很好:

Six years later and I came across a similar issue, when trying to decide which backend was available to use.
This code snippet works well for me:

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

Using: GTKAgg

您可以推断,交换backend就像在强制新的backend

As you can deduce, swapping the backend is as simple as re-importing matplotlib.pyplot after forcing the new backend

matplotlib.use('WXAgg',warn=False, force=True)
from matplotlib import pyplot as plt
print "Switched to:",matplotlib.get_backend()

Switched to: WXAgg

对于仍然遇到问题的人,此代码将打印出:
非Gui后端列表;
Gui后端列表;
然后尝试使用每个Gui后端来查看它是否存在并正常运行.

For those still having trouble, this code will print out the:
list of Non Gui backends;
the list of Gui backends;
and then attempt to use each Gui backend to see if it is present and functioning.

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/Python中更改后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆