List of all available matplotlib backends

后端 未结 7 1998
情话喂你
情话喂你 2020-11-27 13:06

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 ba

相关标签:
7条回答
  • 2020-11-27 13:31

    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
    
    0 讨论(0)
提交回复
热议问题