Anekdotin
Anekdotin

Reputation: 1591

PYQT and embedding matplotlib: Graph not showing

Hello im trying to add a custom graph to the pyqt interface I have. Its not showing any data, but the placeholder matplotlib graph is showing. Any help?! Also If i plot just the graph data without putting it into PYQT, it shows up. Thanks!

class MyMplCanvas(FigureCanvas):
"""Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
def __init__(self, parent=None, width=5, height=4, dpi=100):
    fig = Figure(figsize=(width, height), dpi=dpi)
    #self.axes = fig.add_subplot(111)
    # We want the axes cleared every time plot() is called
    self.axes.hold(False)

    self.compute_initial_figure()
    FigureCanvas.__init__(self, fig)
    self.setParent(parent)

    FigureCanvas.setSizePolicy(self,
                               QtGui.QSizePolicy.Expanding,
                               QtGui.QSizePolicy.Expanding)
    FigureCanvas.updateGeometry(self)

def compute_initial_figure(self):
    pass


class MyStaticMplCanvas(MyMplCanvas):

def mystatic(self, parent=None):
    super(MyStaticMplCanvas, self).__init__(parent)

    rate,data = read('test.wav') # reading
    subplot(411)
    self.plot(range(len(data)),data)
    subplot(412)
    self.specgram(data, NFFT=128, noverlap=0) # small window
    subplot(413)
    self.specgram(data, NFFT=512, noverlap=0) 
    subplot(414)
    self.specgram(data, NFFT=1024, noverlap=0) # big window

    self.show()


class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        l = QtGui.QVBoxLayout(self.main_widget)
        sc = MyStaticMplCanvas(self.main_widget, width=5, height=4, dpi=100)

        l.addWidget(sc)

Upvotes: 1

Views: 1269

Answers (1)

Jean-Sébastien
Jean-Sébastien

Reputation: 2697

In the code sample you provided, there is no call to the mystatic method that you use to plot your data. Therefore, nothing is plotted on your figure.

Moreover, It seems you are using the pyplot interface for plotting your data by calling directly subplot and plot for example in mystatic. When embedding a mpl figure in an application, it is recommended from the mpl documentation to stick to the object oriented API.

I've produced a minimal working example from the code you've provided that follows the guidelines provided in the aforementioned documentation. Hope it helps.

from PyQt4 import QtGui
import sys
import numpy as np
import matplotlib as mpl    
mpl.use('Qt4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas

class MyMplCanvas(FigureCanvas):
    def __init__(self, parent=None, width=5, height=4, dpi=100):        
        fig = mpl.figure.Figure(figsize=(width, height), dpi=dpi)   
        fig.set_tight_layout('tight')
        super(MyMplCanvas, self).__init__(fig)

class MyStaticMplCanvas(MyMplCanvas):

    def mystatic(self, parent=None):

        x, y = np.random.rand(50), np.random.rand(50)
        ax1 = self.figure.add_subplot(411)
        ax1.plot(x,y, '.')
        ax2 = self.figure.add_subplot(412)
        ax2.plot(x,y, '.')
        ax3 = self.figure.add_subplot(413)
        ax3.plot(x,y, '.')
        ax4 = self.figure.add_subplot(414)
        ax4.plot(x,y, '.')

if __name__ == '__main__' :

    app = QtGui.QApplication(sys.argv)

    w = MyStaticMplCanvas(width=5, height=6, dpi=100)
    w.mystatic()
    w.show()

    sys.exit(app.exec_()) 

which results in:

enter image description here

Upvotes: 2

Related Questions