将matplotlib绘图嵌入pyqt的方法示例

所属分类: 脚本专栏 / python 阅读数: 1930
收藏 0 赞 0 分享

我的终极整理,供参考

# coding:utf-8
import matplotlib
# 使用 matplotlib中的FigureCanvas (在使用 Qt5 Backends中 FigureCanvas继承自QtWidgets.QWidget)
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtWidgets import QDialog, QPushButton, QVBoxLayout
import matplotlib.pyplot as plt
import numpy as np
import sys
"""学好pyplot API和面向对象 API搞定matplotlib绘图显示在GUI界面上"""
 
class Main_window(QDialog):
  def __init__(self):
    super().__init__()
    # 三步走,定Figure,定Axes,定FigureCanvas
    # 1 直接一段代码搞定figure和axes
    self.figure, (self.ax1, self.ax2) = plt.subplots(figsize=(13, 3), ncols=2)
 
    # 2 先创建figure再创建axes
    # 2.1 用plt.figure() / Figure() 创建figure, 推荐前者
    self.figure = plt.figure(figsize=(5,3), facecolor='#FFD7C4')
    # self.figure = Figure(figsize=(5,3), facecolor='#FFD7C4')
    # 2.2 用plt.subplots() / plt.add_subplot() 创建axes, 推荐前者
    (self.ax1, self.ax2) = self.figure.subplots(1, 2)
    # ax1 = self.figure.add_subplot(121)
    # ax2 = self.figure.add_subplot(122)
 
    # 3 绑定figure到canvas上
    self.canvas = FigureCanvas(self.figure)
 
    self.button_draw = QPushButton("绘图")
    self.button_draw.clicked.connect(self.Draw)
 
    # 设置布局
    layout = QVBoxLayout()
    layout.addWidget(self.canvas)
    layout.addWidget(self.button_draw)
    self.setLayout(layout)
 
  def Draw(self):
    AgeList = ['10', '21', '12', '14', '25']
    NameList = ['Tom', 'Jon', 'Alice', 'Mike', 'Mary']
    # 将AgeList中的数据转化为int类型
    AgeList = list(map(int, AgeList))
 
    # 将x,y转化为numpy数据类型,对于matplotlib很重要
    self.x = np.arange(len(NameList)) + 1
    self.y = np.array(AgeList)
 
    # tick_label后边跟x轴上的值,(可选选项:color后面跟柱型的颜色,width后边跟柱体的宽度)
    self.ax1.bar(range(len(NameList)), AgeList, tick_label=NameList, color='green', width=0.5)
    for a, b in zip(self.x, self.y):
      self.ax1.text(a-1, b, '%d' % b, ha='center', va='bottom')
    plt.title("Demo")
 
    pos = self.ax2.imshow(np.random.random((100, 100)), cmap=plt.cm.BuPu_r)
    self.figure.colorbar(pos, ax=self.ax2)   # 终于可以用colorbar了
 
    self.canvas.draw()
 
 
# 运行程序
if __name__ == '__main__':
  app = QtWidgets.QApplication(sys.argv)
  main_window = Main_window()
  main_window.show()
  app.exec()

总结就是,想要在特定的位置放matplotlib绘图还是要用面向对象的API,但混合使用pyplot的API可以使代码更简单。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Python实现图像几何变换

这篇文章主要介绍了Python实现图像几何变换的方法,实例分析了Python基于Image模块实现图像翻转、旋转、改变大小等操作的相关技巧,非常简单实用,需要的朋友可以参考下
收藏 0 赞 0 分享

Python中的urllib模块使用详解

这篇文章主要介绍了Python中的urllib模块使用详解,是Python入门学习中的基础知识,需要的朋友可以参考下
收藏 0 赞 0 分享

Python的多态性实例分析

这篇文章主要介绍了Python的多态性,以实例形式深入浅出的分析了Python在面向对象编程中多态性的原理与实现方法,需要的朋友可以参考下
收藏 0 赞 0 分享

python生成IP段的方法

这篇文章主要介绍了python生成IP段的方法,涉及Python文件读写及随机数操作的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python操作redis的方法

这篇文章主要介绍了python操作redis的方法,包括Python针对redis的连接、设置、获取、删除等常用技巧,具有一定参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

python妹子图简单爬虫实例

这篇文章主要介绍了python妹子图简单爬虫,实例分析了Python爬虫程序所涉及的页面源码获取、进度显示、正则匹配等技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

分析用Python脚本关闭文件操作的机制

这篇文章主要介绍了分析用Python脚本关闭文件操作的机制,作者分Python2.x版本和3.x版本两种情况进行了阐述,需要的朋友可以参考下
收藏 0 赞 0 分享

python实现搜索指定目录下文件及文件内搜索指定关键词的方法

这篇文章主要介绍了python实现搜索指定目录下文件及文件内搜索指定关键词的方法,可实现针对文件夹及文件内关键词的搜索功能,需要的朋友可以参考下
收藏 0 赞 0 分享

python中getaddrinfo()基本用法实例分析

这篇文章主要介绍了python中getaddrinfo()基本用法,实例分析了Python中使用getaddrinfo方法进行IP地址解析的基本技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

python查找指定具有相同内容文件的方法

这篇文章主要介绍了python查找指定具有相同内容文件的方法,涉及Python针对文件操作的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多