matplotlib与pyqt5结合学习笔记

关于matplotlib的面向对象学习记录

关于这篇文章:由于编写pyqt5应用需要嵌入figure绘制图像,需要进一步了解matplotlib的构成,故记录如下。

一 、关键的类

在常见的使用中,我们一般使用plt.plot(x,y,‘r.’)或者subplot这类方法隐含的创建了figure,axes这些对象,为了从面向对象角度认识学习matplotlib,我们需要了解清楚各个类的关系和各自作用。

一张图说明各个类的继承关系:
matplotlib与pyqt5结合学习笔记_第1张图片

如果看到这可能还是不够明白我们关注的重点,再来一张图:
一张我们经常绘制(打交道)的图
思考联系我们日常画画的情景:一张纸(canvas,决定了我们绘制图像的地方)、一支笔(render,负责具体图像的渲染)绘制出图像(artist,我们创建的图像元素,大到figure,小到tick这些都是artist,我们作为“artist”画出来的都是artist)。

所以绘制一幅图,我们关心的重点类如下:
1、backend(介绍见下文,此处以基类为例):
matplotlib.backend_bases.FigureCanvasBase(figure)
matplotlib.backend_bases.RendererBase
2、artist
大多数类的基类:Artist类
官方链接:https://matplotlib.org/api/artist_api.html#matplotlib.artist.Artist

Abstract base class for someone who renders into a FigureCanvas

官方教程一段话说的很清楚

There are three layers to the matplotlib API.

the matplotlib.backend_bases.FigureCanvas is the area onto which the
figure is drawn
the matplotlib.backend_bases.Renderer is the objectwhich knows how to draw on the FigureCanvas and the
matplotlib.artist.Artist is the object that knows how to use a renderer to paint onto the canvas.

其他如Figure、axes这些类
各个类的详细资料(官方): https://matplotlib.org/api/api_overview.html

二、关于 backend

the “frontend” is the user facing code, i.e., the plotting code,
whereas the “backend” does all the hard work behind-the-scenes to
make the figure. There are two types of backends: user interface
backends
(for use in pygtk, wxpython, tkinter, qt4, or macosx; also
referred to as “interactive backends”) and hardcopy backends
to make image files (PNG, SVG, PDF, PS; also referred to as
“non-interactive backends”).

一般来说我们安装完python和matplotlib后,默认的backend不用设置修改,对于一些特殊需求(如GUI编写)我们可以修改默认的backend。

To make things a little more customizable for graphical user
interfaces, matplotlib separates the concept of the renderer (the
thing that actually does the drawing) from the canvas (the place
where the drawing goes).

1、renderer: 用于渲染我们的图形对象(如Artist的子类),如Agg (Anti-Grain Geometry),这也是一种raster renderers(非矢量绘图,与屏幕dpi有关),或者Vector renderers,矢量绘图,比如Cario。
每次我们重新绘制图像的时候,如plt.show(),实际上是使用render进行图像界面的重新渲染绘制,对于对象方法的绘图调用,如使用axes.plot,需要调用axes.draw,完成渲染绘制(或者draw_artist()方法,绘制部分对象,这些都是render(如agg)的工作)
2、canvas: the place where the drawing goes.

对于backend的深入理解请看具体官方接口文档。
backend的基类: https://matplotlib.org/api/backend_bases_api.html?highlight=backend#module-matplotlib.backend_bases

举例、

matplotlib.use('Qt5Agg')
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
……
self.fig = Figure((5.0, 4.0), dpi=self.dpi)	# 创建figure对象
self.canvas = FigureCanvas(self.fig)	# 对figure进行封装得到canvas对象,这也是我们的figure渲染绘制的地方(就像名字一样,画布)
self.canvas.setParent(self)	# 这里实际上是将创建的canvas放置在self(这里自定义的组件)

更多的资料补充(官方api):
backend的基类:

更多关于Qt5Agg的使用

三、总结

当我们使用matplotlib的时候,首先思考我们的应用场景,明白我们需要控制的参量是哪些,定位到具体的类。
一般说来,如果是日常使用,只需要关注Figure、axes这些类,我们操作最多的应该是axes类,常见的plot、line、hist这些方法都是通过axes调用的(同时可以区别render、draw与show的联系)。
另外,明白各个类的继承关系(区别于绘制的关系,如artist是大多数绘制对象的基类,axes是放在figure上的,但axes不是figure的子类,可以看上图)。
当我们要控制具体渲染,请设置backend,一些底层的相应如鼠标的响应这些都可以进行控制。

你可能感兴趣的:(python学习记录)