Development Tip

사용 가능한 모든 matplotlib 백엔드 목록

yourdevel 2020. 11. 21. 09:09
반응형

사용 가능한 모든 matplotlib 백엔드 목록


현재 백엔드 이름은 다음을 통해 액세스 할 수 있습니다.

>>> matplotlib.pyplot을 plt로 가져 오기
>>> plt.get_backend ()
'GTKAgg'

특정 머신에서 사용할 수있는 모든 백엔드 목록을 가져 오는 방법이 있습니까?


목록에 액세스 할 수 있습니다.

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

세 번째는 이전 두 개의 연결입니다. 소스 코드를 올바르게 읽으면 해당 목록은 하드 코딩되어 있으며 실제로 사용할 수있는 백엔드를 알려주지 않습니다. 도 있습니다

matplotlib.rcsetup.validate_backend(name)

그러나 이것은 또한 하드 코딩 된 목록에 대해서만 확인합니다.


다음은 이전에 게시 된 스크립트를 수정 한 것입니다. 지원되는 모든 백엔드를 찾아 유효성을 검사하고 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 :(")

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.

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

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

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

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

You can pretend to put a wrong backend argument, then it will return you a ValueError with the list of valid matplotlib backends, like this:

Input:

import matplotlib
matplotlib.use('WRONG_ARG')

Output:

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']

참고URL : https://stackoverflow.com/questions/5091993/list-of-all-available-matplotlib-backends

반응형