Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

如何在子图中加上鼠标点击和滚轮事件

下列代码如何在子图中加上鼠标点击和滚轮事件

--coding:utf-8--

from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
import sys
import numpy as np

from testplot2pyqt5 import Ui_Dialog

import matplotlib
matplotlib.use("Qt5Agg") # 声明使用QT5
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_Dialog(object):

def setupUi(self, Dialog):
    Dialog.setObjectName("Dialog")
    Dialog.resize(718, 515)
    self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
    self.buttonBox.setGeometry(QtCore.QRect(370, 470, 341, 32))
    self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
    self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
    self.buttonBox.setObjectName("buttonBox")
    self.widget = QtWidgets.QWidget(Dialog)
    self.widget.setGeometry(QtCore.QRect(10, 10, 691, 451))
    self.widget.setObjectName("widget")
    self.groupBox = QtWidgets.QGroupBox(self.widget)
    self.groupBox.setGeometry(QtCore.QRect(0, 0, 691, 451))
    self.groupBox.setObjectName("groupBox")

    self.retranslateUi(Dialog)
    self.buttonBox.accepted.connect(Dialog.accept)
    self.buttonBox.rejected.connect(Dialog.reject)
    QtCore.QMetaObject.connectSlotsByName(Dialog)

def retranslateUi(self, Dialog):
    _translate = QtCore.QCoreApplication.translate
    Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
    self.groupBox.setTitle(_translate("Dialog", "GroupBox_Matplotlib的图形显示:"))

创建一个matplotlib图形绘制类

class MyFigure(FigureCanvas):

def __init__(self,width=5, height=4, dpi=100):
    #第一步:创建一个创建Figure
    self.fig = Figure(figsize=(width, height), dpi=dpi)
    #第二步:在父类中激活Figure窗口
    super(MyFigure,self).__init__(self.fig) #此句必不可少,否则不能显示图形
    #第三步:创建一个子图,用于绘制图形用,111表示子图编号,如matlab的subplot(1,1,1)
    self.axes = self.fig.add_subplot(111)
#第四步:就是画图,【可以在此类中画,也可以在其它类中画】
def plotsin(self):
    self.axes0 = self.fig.add_subplot(111)
    t = np.arange(0.0, 3.0, 0.01)
    s = np.sin(2 * np.pi * t)
    self.axes0.plot(t, s)
def plotcos(self):
    t = np.arange(0.0, 3.0, 0.01)
    s = np.sin(2 * np.pi * t)
    self.axes.plot(t, s)

class MainDialogImgBW(QDialog,Ui_Dialog):

def __init__(self):
    super(MainDialogImgBW,self).__init__()
    self.setupUi(self)
    self.setWindowTitle("显示matplotlib绘制图形")
    self.setMinimumSize(0,0)

    #第五步:定义MyFigure类的一个实例
    self.F = MyFigure(width=3, height=2, dpi=100)
    #self.F.plotsin()
    self.plotcos()
    #第六步:在GUI的groupBox中创建一个布局,用于添加MyFigure类的实例(即图形)后其他部件。
    self.gridlayout = QGridLayout(self.groupBox)  # 继承容器groupBox
    self.gridlayout.addWidget(self.F,0,1)

    #补充:另创建一个实例绘图并显示
    self.plotother()
    cid = self.F.canvas.mpl_connect('button_press_event', self.onclick)

def plotcos(self):
    t = np.arange(0.0, 5.0, 0.01)
    s = np.cos(2 * np.pi * t)
    self.F.axes.plot(t, s)
    self.F.fig.suptitle("cos")
def plotother(self):
    F1 = MyFigure(width=5, height=4, dpi=100)
    F1.fig.suptitle("Figuer_4")
    F1.axes1 = F1.fig.add_subplot(221)
    x = np.arange(0, 50)
    y = np.random.rand(50)
    F1.axes1.hist(y, bins=50)
    F1.axes1.plot(x, y)
    F1.axes1.bar(x, y)
    F1.axes1.set_title("hist")
    F1.axes2 = F1.fig.add_subplot(222)

    ## 调用figure下面的add_subplot方法,类似于matplotlib.pyplot下面的subplot方法
    x = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    y = [23, 21, 32, 13, 3, 132, 13, 3, 1]
    F1.axes2.plot(x, y)
    F1.axes2.set_title("line")
    # 散点图
    F1.axes3 = F1.fig.add_subplot(223)
    F1.axes3.scatter(np.random.rand(20), np.random.rand(20))
    F1.axes3.set_title("scatter")
    # 折线图
    F1.axes4 = F1.fig.add_subplot(224)
    x = np.arange(0, 5, 0.1)
    F1.axes4.plot(x, np.sin(x), x, np.cos(x))
    F1.axes4.set_title("sincos")
    self.gridlayout.addWidget(F1, 0, 2)
def onclick(self,event):
    '''
    Event handler for button_press_event
    @param event MouseEvent
    '''
    print ("hhh")

if name == "__main__":

app = QApplication(sys.argv)
main = MainDialogImgBW()
main.show()
#app.installEventFilter(main)
sys.exit(app.exec_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...