
matplotlib.backends.backend_qt5agg.FigureCanvasQTagg构成及用法
发布日期:2021-05-07 14:29:34
浏览次数:16
分类:精选文章
本文共 3561 字,大约阅读时间需要 11 分钟。
-
分解
matplotlib:一个绘图用的Python模块
backends:matplotlib中的一个模块,后端。
backend_qt5agg:backends里面的一个模块,里面有两个类:FigureCanvasQTAgg
和_BackendQT5Agg
FigureCanvasQTagg:backend_qt5agg里的一个类
-
FigureCanvasQTagg应用
matplotlib绘制的图可以应用在PyQt5应用里面,下面三个import都需要:
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvasfrom matplotlib.figure import Figureimport matplotlib.pyplot as plt
**matplotlib.backends.backend_qt5agg用来连接Matplotlib和PyQt5。**backend_qt5agg.py的原文如下:
"""Render to qt from agg."""import ctypesfrom matplotlib.transforms import Bboxfrom .. import cbookfrom .backend_agg import FigureCanvasAggfrom .backend_qt5 import ( QtCore, QtGui, QtWidgets, _BackendQT5, FigureCanvasQT, FigureManagerQT, NavigationToolbar2QT, backend_version)from .qt_compat import QT_APIclass FigureCanvasQTAgg(FigureCanvasAgg, FigureCanvasQT): def __init__(self, figure): # Must pass 'figure' as kwarg to Qt base class. super().__init__(figure=figure) def paintEvent(self, event): """Copy the image from the Agg canvas to the qt.drawable. In Qt, all drawing should be done inside of here when a widget is shown onscreen. """ if self._update_dpi(): # The dpi update triggered its own paintEvent. return self._draw_idle() # Only does something if a draw is pending. # If the canvas does not have a renderer, then give up and wait for # FigureCanvasAgg.draw(self) to be called. if not hasattr(self, 'renderer'): return painter = QtGui.QPainter(self) rect = event.rect() left = rect.left() top = rect.top() width = rect.width() height = rect.height() # See documentation of QRect: bottom() and right() are off by 1, so use # left() + width() and top() + height(). bbox = Bbox( [[left, self.renderer.height - (top + height * self._dpi_ratio)], [left + width * self._dpi_ratio, self.renderer.height - top]]) reg = self.copy_from_bbox(bbox) buf = cbook._unmultiplied_rgba8888_to_premultiplied_argb32( memoryview(reg)) # clear the widget canvas painter.eraseRect(rect) qimage = QtGui.QImage(buf, buf.shape[1], buf.shape[0], QtGui.QImage.Format_ARGB32_Premultiplied) if hasattr(qimage, 'setDevicePixelRatio'): # Not available on Qt4 or some older Qt5. qimage.setDevicePixelRatio(self._dpi_ratio) origin = QtCore.QPoint(left, top) painter.drawImage(origin / self._dpi_ratio, qimage) # Adjust the buf reference count to work around a memory # leak bug in QImage under PySide on Python 3. if QT_API in ('PySide', 'PySide2'): ctypes.c_long.from_address(id(buf)).value = 1 self._draw_rect_callback(painter) painter.end() def blit(self, bbox=None): """Blit the region in bbox. """ # If bbox is None, blit the entire canvas. Otherwise # blit only the area defined by the bbox. if bbox is None and self.figure: bbox = self.figure.bbox # repaint uses logical pixels, not physical pixels like the renderer. l, b, w, h = [pt / self._dpi_ratio for pt in bbox.bounds] t = b + h self.repaint(l, self.renderer.height / self._dpi_ratio - t, w, h) def print_figure(self, *args, **kwargs): super().print_figure(*args, **kwargs) self.draw()@_BackendQT5.exportclass _BackendQT5Agg(_BackendQT5): FigureCanvas = FigureCanvasQTAgg
-
Reference
发表评论
最新留言
感谢大佬
[***.8.128.20]2025年03月25日 06时30分09秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
上周热点回顾(6.25-7.1)
2021-05-09
【故障公告】10:30-10:45 左右 docker swarm 集群节点问题引发故障
2021-05-09
工作半年的思考
2021-05-09
不可思议的纯 CSS 滚动进度条效果
2021-05-09
【CSS进阶】伪元素的妙用--单标签之美
2021-05-09
开始CN的生活
2021-05-09
惊闻NBC在奥运后放弃使用Silverlight
2021-05-09
IE下尚未实现错误的原因
2021-05-09
Kubernetes 学习系列文章
2021-05-09
创建自己的Docker基础镜像
2021-05-09
使用Jenkins来实现内部的持续集成流程(上)
2021-05-09
HTTP 协议图解
2021-05-09
Python 简明教程 --- 20,Python 类中的属性与方法
2021-05-09
Python 简明教程 --- 21,Python 继承与多态
2021-05-09
KNN 算法-理论篇-如何给电影进行分类
2021-05-09
Spring Cloud第九篇 | 分布式服务跟踪Sleuth
2021-05-09
CODING 敏捷实战系列课第三讲:可视化业务分析
2021-05-09
使用 CODING DevOps 全自动部署 Hexo 到 K8S 集群
2021-05-09
工作动态尽在掌握 - 使用 CODING 度量团队效能
2021-05-09
CODING DevOps 代码质量实战系列最后一课,周四发车
2021-05-09