在Matplotlib中,如何查看可用的输出格式列表
根据Matplotlib的说明,matplotlib.figure.save_fig
这个函数可以接受一个可选的参数format
(具体可以查看Matplotlib的文档)。
这个参数可以是“当前使用的后端支持的文件扩展名中的一个”(这是官方文档里的说法)。
我想问的是:怎么才能知道,对于某个特定的后端,支持哪些文件扩展名呢?
可以通过matplotlib.rcsetup.all_backends
来查看可用的后端。这些后端在matplotlib.backends
中可以找到,但我找不到获取支持的扩展名的方法。
3 个回答
0
这里有一个渲染器和文件类型的列表:http://matplotlib.sourceforge.net/faq/installing_faq.html#what-is-a-backend。除此之外,每个渲染器都有一个叫做 get_supported_filetypes
的方法,这个方法在它们各自的 FigureCanvas{backend-name}
类里,可以用来查看支持的文件格式列表。
4
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"
。有没有更简单的方法直接指向正确的后端模块呢?
42
如果你创建了一个图形,你可以通过画布对象来获取支持的文件格式:
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'
}
这样你就能看到可以用来输出你当前图形的所有格式了。