所有可用的matplotlib后端列表。

时间:2022-11-18 16:58:17

The current backend name is accessible via

可以通过以下方式访问当前后端名称

>>> import matplotlib.pyplot as plt
>>> plt.get_backend()
'GTKAgg'

Is there a way to get a list of all backends that can be used on a particular machine?

是否有一种方法可以获得可以在特定机器上使用的所有后台的列表?

Thanks in advance.

提前谢谢。

5 个解决方案

#1


36  

You can access the lists

您可以访问列表

matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends

the third being the concatenation of the former two. If I read the source code correctly, those lists are hard-coded though, and don't tell you what backends are actually usable. There is also

第三个是前两个的串联。如果我正确地阅读了源代码,那么这些列表是硬编码的,并且不会告诉您什么后端实际上是可用的。也有

matplotlib.rcsetup.validate_backend(name)

but this also only checks against the hard-coded list.

但这也只对硬编码的列表进行检查。

#2


41  

Here is a modification of the script posted previously. It finds all supported backends, validates them and measures their fps. On OSX it crashes python when it comes to tkAgg, so use at your own risk ;)

这里是对之前发布的脚本的修改。它查找所有受支持的后端,验证它们并度量它们的fps。在OSX上,当涉及到tkAgg时,它会崩溃,所以在你自己的风险中使用它;

from pylab import *
import time

import matplotlib.backends
import matplotlib.pyplot as p
import os.path


def is_backend_module(fname):
    """Identifies if a filename is a matplotlib backend module"""
    return fname.startswith('backend_') and fname.endswith('.py')

def backend_fname_formatter(fname): 
    """Removes the extension of the given filename, then takes away the leading 'backend_'."""
    return os.path.splitext(fname)[0][8:]

# get the directory where the backends live
backends_dir = os.path.dirname(matplotlib.backends.__file__)

# filter all files in that directory to identify all files which provide a backend
backend_fnames = filter(is_backend_module, os.listdir(backends_dir))

backends = [backend_fname_formatter(fname) for fname in backend_fnames]

print("supported backends: \t" + str(backends))

# validate backends
backends_valid = []
for b in backends:
    try:
        p.switch_backend(b)
        backends_valid += [b]
    except:
        continue

print("valid backends: \t" + str(backends_valid))


# try backends performance
for b in backends_valid:

    ion()
    try:
        p.switch_backend(b)


        clf()
        tstart = time.time()               # for profiling
        x = arange(0,2*pi,0.01)            # x-array
        line, = plot(x,sin(x))
        for i in arange(1,200):
            line.set_ydata(sin(x+i/10.0))  # update the data
            draw()                         # redraw the canvas

        print(b + ' FPS: \t' , 200/(time.time()-tstart))
        ioff()

    except:
        print(b + " error :(")

#3


7  

There is the hard-coded list mentioned by Sven, but to find every backend which Matplotlib can use (based on the current implementation for setting up a backend) the matplotlib/backends folder can be inspected.

Sven提到了硬编码列表,但是要找到Matplotlib可以使用的每个后端(基于当前的实现来设置后端),可以检查Matplotlib /backends文件夹。

The following code does this:

下面的代码是这样的:

import matplotlib.backends
import os.path

def is_backend_module(fname):
    """Identifies if a filename is a matplotlib backend module"""
    return fname.startswith('backend_') and fname.endswith('.py')

def backend_fname_formatter(fname): 
    """Removes the extension of the given filename, then takes away the leading 'backend_'."""
    return os.path.splitext(fname)[0][8:]

# get the directory where the backends live
backends_dir = os.path.dirname(matplotlib.backends.__file__)

# filter all files in that directory to identify all files which provide a backend
backend_fnames = filter(is_backend_module, os.listdir(backends_dir))

backends = [backend_fname_formatter(fname) for fname in backend_fnames]

print backends

#4


3  

You can also see some documentation for a few backends here:

你也可以在这里看到一些文件的支持:

http://matplotlib.org/api/index_backend_api.html

http://matplotlib.org/api/index_backend_api.html

the pages lists just a few backends, some of them don't have a proper documentation:

这些页面只列出了几个后端,其中一些没有合适的文档:

matplotlib.backend_bases
matplotlib.backends.backend_gtkagg
matplotlib.backends.backend_qt4agg
matplotlib.backends.backend_wxagg
matplotlib.backends.backend_pdf
matplotlib.dviread
matplotlib.type1font

#5


2  

You could look at the following folder for a list of possible backends...

您可以查看下面的文件夹,找到可能的后端……

/Library/Python/2.6/site-packages/matplotlib/backends
/usr/lib64/Python2.6/site-packages/matplotlib/backends

#1


36  

You can access the lists

您可以访问列表

matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends

the third being the concatenation of the former two. If I read the source code correctly, those lists are hard-coded though, and don't tell you what backends are actually usable. There is also

第三个是前两个的串联。如果我正确地阅读了源代码,那么这些列表是硬编码的,并且不会告诉您什么后端实际上是可用的。也有

matplotlib.rcsetup.validate_backend(name)

but this also only checks against the hard-coded list.

但这也只对硬编码的列表进行检查。

#2


41  

Here is a modification of the script posted previously. It finds all supported backends, validates them and measures their fps. On OSX it crashes python when it comes to tkAgg, so use at your own risk ;)

这里是对之前发布的脚本的修改。它查找所有受支持的后端,验证它们并度量它们的fps。在OSX上,当涉及到tkAgg时,它会崩溃,所以在你自己的风险中使用它;

from pylab import *
import time

import matplotlib.backends
import matplotlib.pyplot as p
import os.path


def is_backend_module(fname):
    """Identifies if a filename is a matplotlib backend module"""
    return fname.startswith('backend_') and fname.endswith('.py')

def backend_fname_formatter(fname): 
    """Removes the extension of the given filename, then takes away the leading 'backend_'."""
    return os.path.splitext(fname)[0][8:]

# get the directory where the backends live
backends_dir = os.path.dirname(matplotlib.backends.__file__)

# filter all files in that directory to identify all files which provide a backend
backend_fnames = filter(is_backend_module, os.listdir(backends_dir))

backends = [backend_fname_formatter(fname) for fname in backend_fnames]

print("supported backends: \t" + str(backends))

# validate backends
backends_valid = []
for b in backends:
    try:
        p.switch_backend(b)
        backends_valid += [b]
    except:
        continue

print("valid backends: \t" + str(backends_valid))


# try backends performance
for b in backends_valid:

    ion()
    try:
        p.switch_backend(b)


        clf()
        tstart = time.time()               # for profiling
        x = arange(0,2*pi,0.01)            # x-array
        line, = plot(x,sin(x))
        for i in arange(1,200):
            line.set_ydata(sin(x+i/10.0))  # update the data
            draw()                         # redraw the canvas

        print(b + ' FPS: \t' , 200/(time.time()-tstart))
        ioff()

    except:
        print(b + " error :(")

#3


7  

There is the hard-coded list mentioned by Sven, but to find every backend which Matplotlib can use (based on the current implementation for setting up a backend) the matplotlib/backends folder can be inspected.

Sven提到了硬编码列表,但是要找到Matplotlib可以使用的每个后端(基于当前的实现来设置后端),可以检查Matplotlib /backends文件夹。

The following code does this:

下面的代码是这样的:

import matplotlib.backends
import os.path

def is_backend_module(fname):
    """Identifies if a filename is a matplotlib backend module"""
    return fname.startswith('backend_') and fname.endswith('.py')

def backend_fname_formatter(fname): 
    """Removes the extension of the given filename, then takes away the leading 'backend_'."""
    return os.path.splitext(fname)[0][8:]

# get the directory where the backends live
backends_dir = os.path.dirname(matplotlib.backends.__file__)

# filter all files in that directory to identify all files which provide a backend
backend_fnames = filter(is_backend_module, os.listdir(backends_dir))

backends = [backend_fname_formatter(fname) for fname in backend_fnames]

print backends

#4


3  

You can also see some documentation for a few backends here:

你也可以在这里看到一些文件的支持:

http://matplotlib.org/api/index_backend_api.html

http://matplotlib.org/api/index_backend_api.html

the pages lists just a few backends, some of them don't have a proper documentation:

这些页面只列出了几个后端,其中一些没有合适的文档:

matplotlib.backend_bases
matplotlib.backends.backend_gtkagg
matplotlib.backends.backend_qt4agg
matplotlib.backends.backend_wxagg
matplotlib.backends.backend_pdf
matplotlib.dviread
matplotlib.type1font

#5


2  

You could look at the following folder for a list of possible backends...

您可以查看下面的文件夹,找到可能的后端……

/Library/Python/2.6/site-packages/matplotlib/backends
/usr/lib64/Python2.6/site-packages/matplotlib/backends