2011-09-30 72 views
25

根据Matplotlib文档,matplotlib.figure.save_fig采用可选参数format(请参阅matplotlib.figure documentation)。在Matplotlib中,有没有办法知道可用输出格式列表

此参数取'活动后端支持的文件扩展名之一'(如官方文档所述)。

我的观点是:如何知道,对于特定的后端,支持的扩展名列表?

可用后端列表可通过matplotlib.rcsetup.all_backends访问。这些后端在matplotlib.backends中可用,但是,我找不到检索受支持扩展的方法。

回答

33

如果你创建一个人物,你可以得到与画布对象的可用支持的文件格式:

import matplotlib.pyplot as plt 
fig = plt.figure() 

print fig.canvas.get_supported_filetypes() 

>>> { 
    'svgz': 'Scalable Vector Graphics', 
    'ps': 'Postscript', 
    'emf': 'Enhanced Metafile', 
    'rgba': 'Raw RGBA bitmap', 
    'raw': 'Raw RGBA bitmap', 
    'pdf': 'Portable Document Format', 
    'svg': 'Scalable Vector Graphics', 
    'eps': 'Encapsulated Postscript', 
    'png': 'Portable Network Graphics' 
} 

,它会列出所有的格式,你可以输出你的当前对象。

+0

不错的一个...没有看到图形对象上的这个画布属性。 – ohe

+0

难以捉摸的魔法,它会在它愈合时燃烧。感谢你!我的Matplotlib工具腰带现在变平了。 –

2

FigureCanvasBase类,位于每个后端有一个get_supported_filetypes方法。

backend_agg

figure = matplotlib.figure.Figure() 
fcb = matplotlib.backends.backend_agg.FigureCanvasBase(figure) 
supported_file_types = fcb.get_supported_filetypes() 

supported_file_types包含:

{'emf': 'Enhanced Metafile', 
'eps': 'Encapsulated Postscript', 
'pdf': 'Portable Document Format', 
'png': 'Portable Network Graphics', 
'ps': 'Postscript', 
'raw': 'Raw RGBA bitmap', 
'rgba': 'Raw RGBA bitmap', 
'svg': 'Scalable Vector Graphics', 
'svgz': 'Scalable Vector Graphics'} 

剩下的一个问题.... matplotlib.get_backend()回报"agg"。有没有更简单的方法直接指向正确的后端模块?

+0

'matplotlib.get_backend()'是常用的方法(对此不太容易?)。如果你想改变后端,你可以用'matplotlib.use'或者在matplotlibrc文件中设置,'matplotlib.matplotlib_fname()'会告诉你配置文件的位置。 – wim

+0

'matplotlib.get_backend()'返回一个原始的'str'而不是实际的后端模块在大多数使用情况下在功能上是无用的。是的,你明显得到了'getattr(matplotlib.backends,'backend_'+ matplotlib.get_backend()。lower())'你的方式来伪成功 - **但你不应该。**使用那种排序痛苦的骇客应该永远是最后的手段。 –

相关问题