I'm trying to develop a very simple pyside/Qt program using matplotlib. I want that a graph is draw when a button is pressed. So far, I can draw something on the constructor, but I can't connect the Pyside event with matplotlib. Is there a way to do that?
import sys
import platform
import numpy as np
import PySide
from PySide.QtGui import QApplication, QMainWindow, QTextEdit,\
QPushButton, QMessageBox, QWidget, QVBoxLayout
from PySide import QtCore
__version__ = '0.0.1'
import matplotlib
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.main_frame = QWidget()
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.canvas.setParent( self.main_frame )
self.axes = self.figure.add_subplot(111)
vbox = QVBoxLayout( )
vbox.addWidget( self.canvas )
self.main_frame.setLayout( vbox )
self.setCentralWidget( self.main_frame )
self.button = QPushButton('Run')
def button_pressed(self):
data1 = np.loadtxt('FStream.dat')
data2 = np.loadtxt('FShield.dat')
self.axes.plot(data1[0],data1[1],data2[0],data2[1])
print 'pressed'
self.canvas.draw()
if __name__ == '__main__':
app = QApplication(sys.argv)
frame = MainWindow()
frame.button.clicked.connect(frame.button_pressed)
frame.button.show()
frame.show()
app.exec_()
Thanks a lot!
EDIT: edited the code to put the draw().
EDIT 2: Separating in different functions is now looking like this:
import sys
import platform
import numpy as np
import PySide
from PySide.QtGui import QApplication, QMainWindow, QTextEdit,\
QPushButton, QMessageBox, QWidget, QVBoxLayout
from PySide import QtCore
__version__ = '0.0.1'
from ui_pygradient_uni import Ui_MainWindow
import matplotlib
import widgets.matplotlibwidget
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
#from matplotlib.figure import Figure
#from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.main_frame = widgets.matplotlibwidget.MatplotlibWidget()
self.button = QPushButton('Run')
vbox = QVBoxLayout( )
vbox.addWidget( self.main_frame.canvas )
self.main_frame.setLayout( vbox )
self.setCentralWidget( self.main_frame )
def button_pressed(self):
data1 = np.loadtxt('FStream.dat')
data2 = np.loadtxt('FShield.dat')
self.axes.plot(data1[0],data1[1],data2[0],data2[1])
self.canvas.draw()
print 'pressed'
if __name__ == '__main__':
app = QApplication(sys.argv)
frame = MainWindow()
frame.button.clicked.connect(frame.main_frame.Plot)
frame.button.show()
frame.show()
app.exec_()
And the matplotlibwidget is like this:
import matplotlib
import numpy as np
matplotlib.use('Qt4Agg')
matplotlib.rcParams['backend.qt4']='PySide'
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
class MatplotlibWidget(FigureCanvas):
def __init__(self, parent=None):
super(MatplotlibWidget, self).__init__(Figure())
self.setParent(parent)
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
self.axes = self.figure.add_subplot(111)
def Plot(self):
data1 = np.loadtxt('FStream.dat')
data2 = np.loadtxt('FShield.dat')
self.axes.plot(data1[0],data1[1],data2[0],data2[1])
self.canvas.draw()