大家好,欢迎来到IT知识分享网。
matplotlib: 有关 Backend 的说明
什么是Backend?
Matplotlib针对许多不同的用例和输出格式。
有些人在python shell中交互式地使用Matplotlib,并在键入命令时弹出绘图窗口。
有些人使用木星笔记本,绘制内联图来快速分析数据。
还有一些人将Matplotlib嵌入到图形用户界面中,如PyQt或PyGObject,以构建丰富的应用程序。
有些人在批处理脚本中使用Matplotlib从数值模拟中生成postscript图像,还有一些人运行web应用程序服务器动态地提供图形。
为了支持所有这些用例,Matplotlib可以针对不同的输出,这些功能中的每一个都被称为后端。
“前端”是面向用户的代码,也就是绘图代码,而“后端”负责所有幕后的工作,以制作图形。
有两种类型的后端:
- 用户界面后端(用于PyQt/PySide、PyGObject、Tkinter、wxPython或macOS/Cocoa);也称为“交互式后端”;
- 硬拷贝后端制作图像文件(PNG, SVG, PDF, PS;也称为“非交互式后端”)。
如何选择一个后端
有三种方式来配置你的后端:
1. 修改matplotlibrc文件中的rcParams[“backend”](默认值:’agg’)参数
2. MPLBACKEND环境变量: 可以将这个可选变量设置为选择Matplotlib后端
3. 使用函数matplotlib.use ()
下面给出了更详细的描述。
如果存在多个配置,则列表中的最后一个优先; 例如,调用matplotlib.use()将覆盖matplotlibrc中的设置。
如果没有显式设置后端,Matplotlib会根据系统上可用的内容以及GUI事件循环是否已经运行自动检测一个可用的后端。
在Linux上,如果没有设置环境变量DISPLAY,则“事件循环”被标识为“headless”,这将导致回退到非交互后端(agg)。
下面是配置方法的详细说明:
1. 修改matplotlibrc文件(路径为: C:\Program Files\Python38\Lib\site-packages\matplotlib\mpl-data)中的rcParams[“backend”](默认值:’agg’)参数.
2. 设置MPLBACKEND环境变量:
您可以为当前shell或单个脚本设置环境变量。
在Unix:
export MPLBACKEND=qt5agg python simple_plot.py MPLBACKEND=qt5agg python simple_plot.py
在Windows上,只有前者是可能的:
set MPLBACKEND=qt5agg python simple_plot.py
设置这个环境变量将覆盖任何matplotlibrc中的后端参数,即使在您当前的工作目录中有matplotlibrc。因此,全局设置MPLBACKEND,例如在你的.bashrc或.profile中,是不鼓励的,因为它可能会导致反直觉的行为。
3. 如果你的脚本依赖于特定的后端,你可以使用函数matplotlib.use():
import matplotlib matplotlib.use('qt5agg')
这应该在创建任何图形之前完成,否则Matplotlib可能无法切换后端并引发ImportError。
如果用户想使用不同的后端,使用use就需要修改代码。因此,除非绝对必要,否则应避免显式调用use。
内装式的后端
默认情况下,Matplotlib应该自动选择一个默认后端,它允许从脚本进行交互工作和绘图,并将输出输出到屏幕和/或文件,因此至少在初始阶段,您不需要担心后端。
最常见的例外是您的Python发行版没有安装tkinter,并且没有安装其他GUI工具包。这发生在某些Linux发行版上,您需要安装名为python-tk(或类似的)的Linux包。
但是,如果您想编写图形用户界面或web应用程序服务器(嵌入到web应用程序服务器中),或者需要更好地理解正在发生的事情,请继续阅读。
为了使图形用户界面的内容更易于定制,Matplotlib将渲染器(实际绘图的东西)的概念与画布(绘图的地方)分离开来。
用户界面的标准渲染器是Agg,它使用反纹理几何c++库来制作图形的栅格(像素)图像; Qt5Agg、Qt4Agg、GTK3Agg、wxAgg、TkAgg和macosx后端使用它。另一种渲染器是基于Cairo库的,由Qt5Cairo、Qt4Cairo等使用。
对于渲染引擎,还可以区分矢量渲染器和栅格渲染器。矢量图形语言会发出像“从这一点到这一点画一条线”这样的绘图命令,因此是无尺度的,而光栅后端会生成直线的像素表示,其精度取决于DPI设置。
下面是Matplotlib渲染器的摘要(每个渲染器都有一个同名的后端;这些是非交互式后端,能够写入文件):
Renderer | Filetypes | Description |
---|---|---|
AGG | png | raster graphics — high quality images using the Anti-Grain Geometry engine |
vector graphics — Portable Document Format | ||
PS | ps, eps | vector graphics — Postscript output |
SVG | svg | vector graphics — Scalable Vector Graphics |
PGF | pgf, pdf | vector graphics — using the pgf package |
Cairo | png, ps, pdf, svg | raster or vector graphics — using the Cairo library |
要使用非交互式后端保存绘图,请使用matplotlib.pyplot.savefig(‘filename’)方法。
这里是支持的用户界面和渲染器组合; 这些是交互式的后端,能够显示到屏幕上,并从上面的表中使用适当的渲染器来写入文件:
Backend | Description |
---|---|
Qt5Agg | Agg rendering in a Qt5 canvas (requires PyQt5). This backend can be activated in IPython with %matplotlib qt5 . |
ipympl | Agg rendering embedded in a Jupyter widget. (requires ipympl). This backend can be enabled in a Jupyter notebook with %matplotlib ipympl . |
GTK3Agg | Agg rendering to a GTK 3.x canvas (requires PyGObject, and pycairo or cairocffi). This backend can be activated in IPython with %matplotlib gtk3 . |
macosx | Agg rendering into a Cocoa canvas in OSX. This backend can be activated in IPython with %matplotlib osx . |
TkAgg | Agg rendering to a Tk canvas (requires TkInter). This backend can be activated in IPython with %matplotlib tk . |
nbAgg | Embed an interactive figure in a Jupyter classic notebook. This backend can be enabled in Jupyter notebooks via %matplotlib notebook . |
WebAgg | On show() will start a tornado server with an interactive figure. |
GTK3Cairo | Cairo rendering to a GTK 3.x canvas (requires PyGObject, and pycairo or cairocffi). |
Qt4Agg | Agg rendering to a Qt4 canvas (requires PyQt4 or pyside ). This backend can be activated in IPython with %matplotlib qt4 . |
wxAgg | Agg rendering to a wxWidgets canvas (requires wxPython 4). This backend can be activated in IPython with %matplotlib wx . |
请注意: 内置后端名称不区分大小写; 例如,’Qt5Agg’和’qt5agg’是等价的。
ipympl
jupiter小部件生态系统发展太快,无法直接在Matplotlib中支持。安装ipympl
pip install ipympl jupyter nbextension enable --py --sys-prefix ipympl
或者
conda install ipympl -c conda-forge
如何选择PyQt4或PySide?
可以将QT_API环境变量设置为pyqt或pyside,分别使用PyQt4或pyside。
因为要使用的绑定的默认值是PyQt4,所以Matplotlib首先尝试导入它。如果导入失败,则尝试导入PySide。
使用non-builtin后端
更一般地说,任何可导入的后端都可以通过使用上面的任何方法来选择。
If name.of.the.backend
is the module containing the backend, use module://name.of.the.backend
as the backend name, e.g. matplotlib.use('module://name.of.the.backend')
.
其他代码
import matplotlib import matplotlib.rcsetup as rcsetup matplotlib.use('Agg', force=True) # 'Agg' matplotlib.get_backend() # 查看当前系统的后端 matplotlib.use('Qt5Agg', force=True) # 'Qt5Agg' matplotlib.get_backend() # 查看当前系统的后端 matplotlib.matplotlib_fname() # 查看matplotlibrc路径 # 'C:\\Program Files\\Python38\\lib\\site-packages\\matplotlib\\mpl-data\\matplotlibrc' print(rcsetup.all_backends) # 查看支持的所有Backend ['GTK3Agg', 'GTK3Cairo', 'MacOSX', 'nbAgg', 'Qt4Agg', 'Qt4Cairo', 'Qt5Agg', 'Qt5Cairo', 'TkAgg', 'TkCairo', 'WebAgg', 'WX', 'WXAgg', 'WXCairo', 'agg', 'cairo', 'pdf', 'pgf', 'ps', 'svg', 'template']
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/146118.html