Matplotlib后台设定的方法及常见问题

  • 问题

        使用Pycharm的Python控制端,绘图时出现图像空白,一直显示未响应,强制关闭后,显示进程已结束,退出代码。

 解决方法。设置Matplotlib的后端为Qt5Agg

import matplotlib
matplotlib.use('Qt5Agg')
  • Matplotlib的前端与后端

        有些人在Python shell中交互式地使用Matplotlib,输入命令后就会弹出绘图窗口。有些人运行Jupyter notebook并绘制内联图来快速进行数据分析。还有人将Matplotlib嵌入到图形用户界面中,如PyQt或PyGObject,以构建丰富的应用程序。有些人在批处理脚本中使用Matplotlib从数值模拟中生成postscript图像,还有一些人运行web应用程序服务器来动态地提供图形。为了支持所有这些用例,Matplotlib可以有不同的输出,每一种功能都被称为后端。

        “前端”是面向用户的代码,即绘图代码,而“后端”在幕后完成所有辛苦的工作以生成图形。后端有两种类型:用户界面后端(用于PyQt/PySide、PyGObject、Tkinter、wxPython或macOS/Cocoa中);也称为“交互式后端”)和硬拷贝后端,用于制作图像文件(PNG、SVG、PDF、PS;也称为“非交互式后端”)。

  • 如何查看当前后端的类型?
import matplotlib 
matplotlib.get_backend()
matplotlib.matplotlib_fname()

        默认的后端是'QtAgg',后一条语句是输出当前Matplotlib的配置文件的路径。
 

  • 配置后端的方式

        有三种办法,第一种是代码配置法。

import matplotlib
matplotlib.use('Qt5Agg')
import matplotlib.pyplot as plt

      第二种是设置环境变量的方法。

#Linux系统中
> export MPLBACKEND=qtagg
> python simple_plot.py
> MPLBACKEND=qtagg python simple_plot.py

#window系统
> set MPLBACKEND=qtagg
> python simple_plot.py

另外也可通过修改Matplotlib的配置文件来实现。

backend : qtagg   # use pyqt with antigrain (agg) rendering

  • Matplotlib内置的后端

非交互式的后端

Renderer

Filetypes

Description

AGG

png

raster graphics -- high quality images using the Anti-Grain Geometry engine

PDF

pdf

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

交互式的后端

Backend

Description

QtAgg

Agg rendering in a Qt canvas (requires PyQt or Qt for Python, a.k.a. PySide). This backend can be activated in IPython with %matplotlib qt.

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.

GTK4Agg

Agg rendering to a GTK 4.x canvas (requires PyGObject, and pycairo or cairocffi). This backend can be activated in IPython with %matplotlib gtk4.

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).

GTK4Cairo

Cairo rendering to a GTK 4.x canvas (requires PyGObject, and pycairo or cairocffi).

wxAgg

Agg rendering to a wxWidgets canvas (requires wxPython 4). This backend can be activated in IPython with %matplotlib wx.

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乐慧100

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值