matplotlib后端对呈现格式有哪些限制? [英] What limits does the matplotlib backend place on rendering formats?

查看:56
本文介绍了matplotlib后端对呈现格式有哪些限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对matplotlib使用的后端在确定可以呈现哪种格式中扮演的角色感到困惑.

例如,文档说"agg"后端会生成PNG,光栅图形",但如果我

  import matplotlibmatplotlib.use(‘agg’)导入matplotlib.pyplot无花果,ax = matplotlib.pyplot.subplots()#... 

我可以使用

  fig.savefig("thefig.pdf") 

生成PDF,或

  fig.savefig("thefig.svg") 

产生矢量图形.

后端在限制matplotlib可以产生哪种格式和种类的渲染(矢量或栅格)中起什么作用?

解决方案

好问题! Agg 后端本身仅生成光栅图形.

调用 fig.savefig('name.pdf')(或 .svg )时发生的情况是后端被临时更改以生成矢量输出./p>

每个后端都可以选择处理方式,但是对于 Agg ,保存矢量输出时,它基本上可以做到:

  pdf = self.switch_backends(FigureCanvasPdf)返回pdf.print_pdf(* args,** kwargs) 

类似地,对于PDF后端,如果要求保存光栅图像,它将暂时切换到Agg.

对于非交互式后端,可以即时进行后端切换,因此该技术被广泛地用于幕后",以允许图形以多种格式保存.


更多细节,以防您正在编写matplotlib后端:任何matplotlib Canvas实例都有几种 print_< format> 方法:

 在[24]中:backend_bases.FigureCanvasBase.print_< tab>backend_bases.FigureCanvasBase.print_bmpbackend_bases.FigureCanvasBase.print_epsbackend_bases.FigureCanvasBase.print_figurebackend_bases.FigureCanvasBase.print_jpegbackend_bases.FigureCanvasBase.print_jpgbackend_bases.FigureCanvasBase.print_pdfbackend_bases.FigureCanvasBase.print_pgfbackend_bases.FigureCanvasBase.print_pngbackend_bases.FigureCanvasBase.print_psbackend_bases.FigureCanvasBase.print_rawbackend_bases.FigureCanvasBase.print_rgbabackend_bases.FigureCanvasBase.print_svgbackend_bases.FigureCanvasBase.print_svgzbackend_bases.FigureCanvasBase.print_tifbackend_bases.FigureCanvasBase.print_tiff 

print_figure 方法通常控制保存.通过查找适当的 print_< formatname> 方法处理保存为特定格式(有关详细信息,请参见 FigureCanvasBase._get_print_method ).

每个后端通过覆盖这些方法来指定可以保存为哪些格式.每个对象的默认设置是暂时切换回相关的基本"后端(例如,栅格格式为Agg,PDF格式为PDF,SVG格式为SVG,等等).这样一来,即使您使用的后端仅支持一种格式,也可以轻松地将图形保存为所有格式.

I'm confused about the role played by the backend used by matplotlib in determining what formats can be rendered.

For example, the documentation says that the 'agg' backend generates PNG, "raster graphics" but if I

import matplotlib
matplotlib.use(‘agg’)

import matplotlib.pyplot
fig, ax = matplotlib.pyplot.subplots()
#...

I can use

fig.savefig("thefig.pdf")

to generate a PDF, or

fig.savefig("thefig.svg")

to produce vector graphics.

What role does the backend play in limiting what formats and kinds of rendering (vector vs. raster) matplotlib can produce?

解决方案

Good question! The Agg backend itself only produces raster graphics.

What's happening when you call fig.savefig('name.pdf') (or .svg) is that the backend is temporarily changed to generate vector output.

Each backend can choose how it handles this, but for Agg, when you save vector output, it basically does:

pdf = self.switch_backends(FigureCanvasPdf)
return pdf.print_pdf(*args, **kwargs)

Similarly, for the PDF backend, it temporarily switches to Agg if asked to save a raster image.

Switching backends can be done on the fly for non-interactive backends, so this technique is used extensively "behind-the-scenes" to allow figures to be saved in multiple formats.


A bit more detail, in case you're ever writing a matplotlib backend: Any matplotlib Canvas instance has several print_<format> methods:

In [24]: backend_bases.FigureCanvasBase.print_<tab>
backend_bases.FigureCanvasBase.print_bmp           
backend_bases.FigureCanvasBase.print_eps           
backend_bases.FigureCanvasBase.print_figure        
backend_bases.FigureCanvasBase.print_jpeg          
backend_bases.FigureCanvasBase.print_jpg           
backend_bases.FigureCanvasBase.print_pdf           
backend_bases.FigureCanvasBase.print_pgf           
backend_bases.FigureCanvasBase.print_png           
backend_bases.FigureCanvasBase.print_ps            
backend_bases.FigureCanvasBase.print_raw           
backend_bases.FigureCanvasBase.print_rgba          
backend_bases.FigureCanvasBase.print_svg           
backend_bases.FigureCanvasBase.print_svgz          
backend_bases.FigureCanvasBase.print_tif           
backend_bases.FigureCanvasBase.print_tiff 

The print_figure method controls saving in general. Saving to a specific format is handled by looking up the approriate print_<formatname> method (See FigureCanvasBase._get_print_method for exact details).

Each backend specifies which formats it can save to by overriding those methods. The default for each is to temporarily switch back to the relevant "base" backend (e.g. Agg for raster formats, PDF for pdf, SVG for svg, etc). This allows figures to be easily saved to all formats even if the backend you're using only supports a single format.

这篇关于matplotlib后端对呈现格式有哪些限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆