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

yipeiwu_com4年前Python基础

我的终极整理,供参考

# 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截取两个单词之间的内容方法

1. __init__ 初始化文件路径,关键字1,关键字2; 2. key_match 使用with open 方法,以二进制方式(也可以改成utf-8,GB2312)读取文件内容(支持...

CentOS 7下安装Python 3.5并与Python2.7兼容并存详解

CentOS 7下安装Python 3.5并与Python2.7兼容并存详解

本文主要给大家介绍了关于在CentOS 7下安装Python 3.5并与Python2.7兼容并存的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: CentOS 7下安装P...

简单学习Python多进程Multiprocessing

简单学习Python多进程Multiprocessing

1.1 什么是 Multiprocessing 多线程在同一时间只能处理一个任务。 可把任务平均分配给每个核,而每个核具有自己的运算空间。 1.2 添加进程 Process 与线程类似,...

python的迭代器与生成器实例详解

本文以实例详解了python的迭代器与生成器,具体如下所示: 1. 迭代器概述:   迭代器是访问集合元素的一种方式。迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问...

Python3 实现串口两进程同时读写

通过两个进程分别读写串口,并把发送与接收到的内容记录在blog中,收到q时程序结束并退出 import threading,time import serial import str...