2017-06-16 12 views
0

2つの行列があり、matplotlibを使ってimshowで表示しています。私はPyQt5を使って小さなGUIにmatplotlibを埋め込んだ。各ブロックは、それぞれマトリックスの各値を表します。私はこれらのブロックをクリック可能にしたい。クリック可能にするのは、各ブロックがクリックされるたびに、別のマットプロットのプロットがポップアップするためです。この段階に進む前に、ブロックをクリック可能にします。どうすればこれを達成できますか?私はインターネットからいくつかの方法を試みましたが、運はありません。matplotlib(imshow)のすべてのブロックをどのようにクリック可能にする必要がありますか?

GUI画面

import sys 
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout, \ 
QLineEdit, QMessageBox, QInputDialog, QLabel, QHBoxLayout, QGridLayout,  QStackedLayout, QFormLayout 
from PyQt5 import QtCore, QtGui, QtWidgets 
import time 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.lines import Line2D 
import numpy as np 
import matplotlib.animation as animation 
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as  NavigationToolbar 
import matplotlib.pyplot as plt 


j=0 
figure = plt.figure() 

H = np.array([[100, 2, 39, 190], [402, 55, 369, 1023], [300, 700, 8, 412], [170, 530, 330, 1]]) 
Z = np.array([[3, 290, 600, 480], [1011, 230, 830, 0], [152, 750, 5, 919], [340, 7, 543, 812]]) 


class Window(QDialog): 
    def __init__(self, parent=None): 
     super(Window, self).__init__(parent) 

     # a figure instance to plot on 
     self.figure = plt.figure() 

     # this is the Canvas Widget that displays the `figure` 
     # it takes the `figure` instance as a parameter to __init__ 
     self.canvas = FigureCanvas(self.figure) 

     # this is the Navigation widget 
     # it takes the Canvas widget and a parent 
     self.toolbar = NavigationToolbar(self.canvas, self) 

     # Just some button connected to `plot` method 
     self.button = QPushButton('Plot') 
     self.button.clicked.connect(self.loop) 
     self.button.setDefault(False) 

     self.stop = QPushButton("Stop") 
     self.stop.clicked.connect(self.loopStop) 
     self.stop.setDefault(False) 

     self.exit = QPushButton('Exit') 
     self.exit.clicked.connect(self.closeIt) 
     self.exit.setDefault(True) 

     self.leBtn = QPushButton('Enter Vmin') 
     self.leBtn.clicked.connect(self.on_pushButtonOK_clicked) 
     self.leBtn.setDefault(False) 

     self.lineedit = QLineEdit(self) 
     validator = QtGui.QIntValidator() 
     self.lineedit.setValidator(validator) 
     self.lineedit.returnPressed.connect(self.leBtn.click) 

     self.leBtn1 = QPushButton('Enter Vmax') 
     self.leBtn1.clicked.connect(self.on_pushButtonOK_clicked1) 
     self.leBtn1.setDefault(False) 

     self.lineedit1 = QLineEdit(self) 
     validator = QtGui.QIntValidator() 
     self.lineedit1.setValidator(validator) 
     self.lineedit1.returnPressed.connect(self.leBtn1.click) 


     # set the layout 
     layout = QFormLayout() 
     layout.addWidget(self.toolbar) 
     layout.addWidget(self.canvas) 
     layout.addWidget(self.button) 
     layout.addWidget(self.stop) 
     layout.addWidget(self.exit) 
     layout.addWidget(self.lineedit) 
     layout.addWidget(self.leBtn) 
     layout.addWidget(self.lineedit1) 
     layout.addWidget(self.leBtn1) 

     self.setLayout(layout) 

    def on_pushButtonOK_clicked(self): 
     self.vmin = int(self.lineedit.text()) 

    def on_pushButtonOK_clicked1(self): 
     self.vmax=int(self.lineedit1.text()) 


    def plot(self): 
     global j 
     if j == 0: 
      j += 1 
      rows, cols = H.shape 

      im = plt.imshow(H, interpolation='nearest', cmap='bwr', vmin=self.vmin, vmax=self.vmax, extent=[0, cols, 0, rows]) 
      v = np.linspace(0, 1023, 15, endpoint=True) 
      ax1 = self.figure.add_axes([0.85, 0.093, 0.04, 0.8]) 
      cax = self.figure.add_subplot(1, 1, 1) 
      self.figure.colorbar(im, cax=ax1, orientation='vertical', ticks=v) 
      self.canvas.draw() 

     elif j == 1: 
      j -= 1 
      rows, cols = H.shape 

      im = plt.imshow(Z, interpolation='nearest', cmap='Spectral', vmin=self.vmin, vmax=self.vmax, 
         extent=[0, cols, 0, rows]) 
      v = np.linspace(0, 1023, 15, endpoint=True) 
      ax1 = self.figure.add_axes([0.85, 0.093, 0.04, 0.8]) 
      cax = self.figure.add_subplot(1, 1, 1) 
      self.figure.colorbar(im, cax=ax1, orientation='vertical', ticks=v) 
      self.canvas.draw() 

    def loop(self): 
     self.timer = QtCore.QTimer() 
     self.timer.setSingleShot(False) 
     self.timer.timeout.connect(self.plot) 
     self.timer.start(1000) 

    def loopStop(self): 
     self.timer.stop() 

    def closeIt(self): 
     self.close() 



if __name__ == '__main__': 
    app = QApplication(sys.argv) 

    main = Window() 
    main.show() 

    sys.exit(app.exec_()) 

答えて

0

あなたはクリックイベントをキャッチしたい場合は、キャンバスとmpl_connect('button_press_event', {some_callback})方法によってそれを行う必要があります。

class Window(QDialog): 
    def __init__(self, parent=None): 
     [...] 
     self.canvas = FigureCanvas(self.figure) 
     self.canvas.mpl_connect('button_press_event', self.on_button_press_event) 
     [...] 

    def on_button_press_event(self, event): 
     print(event) 

例:

import sys 
from PyQt5.QtWidgets import QDialog, QApplication, QPushButton, QVBoxLayout, \ 
QLineEdit, QMessageBox, QInputDialog, QLabel, QHBoxLayout, QGridLayout,  QStackedLayout, QFormLayout 
from PyQt5 import QtCore, QtGui, QtWidgets 
import time 
import matplotlib 
matplotlib.use("TkAgg") 
from matplotlib.lines import Line2D 
import numpy as np 
import matplotlib.animation as animation 
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas 
from matplotlib.backends.backend_qt5agg import NavigationToolbar2QT as  NavigationToolbar 
import matplotlib.pyplot as plt 


j=0 
figure = plt.figure() 

H = np.array([[100, 2, 39, 190], [402, 55, 369, 1023], [300, 700, 8, 412], [170, 530, 330, 1]]) 
Z = np.array([[3, 290, 600, 480], [1011, 230, 830, 0], [152, 750, 5, 919], [340, 7, 543, 812]]) 


class Window(QDialog): 
    def __init__(self, parent=None): 
     super(Window, self).__init__(parent) 

     # a figure instance to plot on 
     self.figure = plt.figure() 

     # this is the Canvas Widget that displays the `figure` 
     # it takes the `figure` instance as a parameter to __init_ 
     self.im = None 
     self.canvas = FigureCanvas(self.figure) 

     self.canvas.mpl_connect('button_press_event', self.on_button_press_event) 

     # this is the Navigation widget 
     # it takes the Canvas widget and a parent 
     self.toolbar = NavigationToolbar(self.canvas, self) 

     self.timer = QtCore.QTimer(self) 
     self.timer.timeout.connect(self.plot) 
     self.timer.setInterval(5000) 

     # Just some button connected to `plot` method 
     self.button = QPushButton('Plot') 
     self.button.clicked.connect(self.timer.start) 
     self.button.setDefault(False) 

     self.stop = QPushButton("Stop") 
     self.stop.clicked.connect(self.timer.stop) 
     self.stop.setDefault(False) 

     self.exit = QPushButton('Exit') 
     self.exit.clicked.connect(self.close) 
     self.exit.setDefault(True) 

     self.leBtn = QPushButton('Enter Vmin') 
     self.leBtn.clicked.connect(self.on_pushButtonOK_clicked) 
     self.leBtn.setDefault(False) 

     self.lineedit = QLineEdit(self) 
     validator = QtGui.QIntValidator() 
     self.lineedit.setValidator(validator) 
     self.lineedit.returnPressed.connect(self.leBtn.click) 

     self.leBtn1 = QPushButton('Enter Vmax') 
     self.leBtn1.clicked.connect(self.on_pushButtonOK_clicked1) 
     self.leBtn1.setDefault(False) 

     self.lineedit1 = QLineEdit(self) 
     validator = QtGui.QIntValidator() 
     self.lineedit1.setValidator(validator) 
     self.lineedit1.returnPressed.connect(self.leBtn1.click) 


     # set the layout 
     layout = QFormLayout() 
     layout.addWidget(self.toolbar) 
     layout.addWidget(self.canvas) 
     layout.addWidget(self.button) 
     layout.addWidget(self.stop) 
     layout.addWidget(self.exit) 
     layout.addWidget(self.lineedit) 
     layout.addWidget(self.leBtn) 
     layout.addWidget(self.lineedit1) 
     layout.addWidget(self.leBtn1) 

     self.setLayout(layout) 

     self.lb = QtWidgets.QLabel(self) 
     self.lb.setWindowFlags(QtCore.Qt.ToolTip) 

    def on_pushButtonOK_clicked(self): 
     self.vmin = int(self.lineedit.text()) 

    def on_pushButtonOK_clicked1(self): 
     self.vmax=int(self.lineedit1.text()) 


    def plot(self): 
     global j 
     if j == 0: 
      j += 1 
      rows, cols = H.shape 

      self.im = plt.imshow(H, interpolation='nearest', cmap='bwr', vmin=self.vmin, vmax=self.vmax, extent=[0, cols, 0, rows]) 
      v = np.linspace(0, 1023, 15, endpoint=True) 
      ax1 = self.figure.add_axes([0.85, 0.093, 0.04, 0.8]) 
      cax = self.figure.add_subplot(1, 1, 1) 
      self.figure.colorbar(self.im, cax=ax1, orientation='vertical', ticks=v) 
      self.canvas.draw() 

     elif j == 1: 
      j -= 1 
      rows, cols = H.shape 

      self.im = plt.imshow(Z, interpolation='nearest', cmap='Spectral', vmin=self.vmin, vmax=self.vmax, 
         extent=[0, cols, 0, rows]) 
      v = np.linspace(0, 1023, 15, endpoint=True) 
      ax1 = self.figure.add_axes([0.85, 0.093, 0.04, 0.8]) 
      cax = self.figure.add_subplot(1, 1, 1) 
      self.figure.colorbar(self.im, cax=ax1, orientation='vertical', ticks=v) 
      self.canvas.draw() 

    def on_button_press_event(self, event): 
     print('button={}, x={}, y={}, xdata={}, ydata={}' 
      .format(event.button, event.x, event.y, event.xdata, event.ydata)) 
     if self.im: 
      message = str(self.im.get_cursor_data(event)) 
      delay = 1000 
      w = self.lb.fontMetrics().width(message) 
      self.lb.resize(w, self.lb.size().height()) 
      self.lb.setText(message) 
      self.lb.move(QtGui.QCursor.pos()) 
      self.lb.show() 
      QtCore.QTimer.singleShot(delay, self.lb.hide) 




if __name__ == '__main__': 
    app = QApplication(sys.argv) 

    main = Window() 
    main.show() 

    sys.exit(app.exec_()) 

注:私は、関数imshowで私を助けるため

+0

おかげでたくさん表示されている値にアクセスできるようにself.imするイム変数を変更しました。少なくとも、私はこのことがどのように機能するのかという考えを得ました。特定のブロックをクリックすると、matplotlibウィンドウにいくつかのランダムなデータがポップアップするはずです。私はちょっとしたアイデアが必要です。ありがとうございました –

+0

私の答えが正しいとマークするのに役立ちます – eyllanesc

+0

私はセルの内容でポップアップを表示して例を更新しました – eyllanesc

関連する問題