所有可用matplotlib后端的列表

Ale*_*sky 64 python matplotlib

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

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

有没有办法获得可以在特定机器上使用的所有后端的列表?

Sve*_*ach 49

您可以访问列表

matplotlib.rcsetup.interactive_bk
matplotlib.rcsetup.non_interactive_bk
matplotlib.rcsetup.all_backends
Run Code Online (Sandbox Code Playgroud)

第三个是前两个的串联.如果我正确读取源代码,那些列表是硬编码的,并不告诉你实际可用的后端.还有

matplotlib.rcsetup.validate_backend(name)
Run Code Online (Sandbox Code Playgroud)

但这也只是检查硬编码列表.


stf*_*tfn 45

这是对先前发布的脚本的修改.它找到所有支持的后端,验证它们并测量它们的fps.在OSX上,当涉及到tkAgg时它会崩溃python,所以使用风险自负;)

from __future__ import print_function, division, absolute_import
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 :(")
Run Code Online (Sandbox Code Playgroud)

  • 好棒!现场绘画!计算帧率并打印!确定哪个后端在您的计算机上最快是非常有用的。 (2认同)

pel*_*son 6

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

以下代码执行此操作:

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
Run Code Online (Sandbox Code Playgroud)


ran*_*man 5

您可以假装输入错误的后端参数,然后它会返回一个 ValueError 并包含有效的 matplotlib 后端列表,如下所示:

输入:

import matplotlib
matplotlib.use('WRONG_ARG')
Run Code Online (Sandbox Code Playgroud)

输出:

ValueError: Unrecognized backend string 'test': valid strings are ['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt
5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
Run Code Online (Sandbox Code Playgroud)