在Matplotlib中,是否有方法知道可用输出表单的列表

2024-05-17 03:19:37 发布

您现在位置:Python中文网/ 问答频道 /正文

根据Matplotlib文档,matplotlib.figure.save_fig接受可选参数format(请参见matplotlib.figure documentation)。

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

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

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


Tags: 文件文档format列表参数官方matplotlibsave
3条回答

以下是呈现程序和文件类型的列表:http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend 除此之外,每个后端在其各自的FigureCanvas{backend-name}类中都有get_supported_filetypes方法,该方法提供了受支持的文件格式列表。

位于每个后端的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"。有没有更简单的方法直接指向正确的后端模块?

如果创建一个图形,则可以使用canvas对象获取可用的受支持文件格式:

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

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

相关问题 更多 >