0

This is what I have adapted my code to with the init functions: I think I'm confused at how inheritance works with graphing applications. I have added my constants to the init function of the figure canvas, but they do not come down to the subclasses. I thought that is how the inheritance works.

import sys
import os
from PyQt4 import QtGui
from PyQt4 import QtCore
import functools
import numpy as np
import random as rd
matplotlib.use("Qt4Agg")
from matplotlib.animation import TimedAnimation
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import serial


class CustomMainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(CustomMainWindow, self).__init__()

        # Define the geometry of the main window
        self.setGeometry(300, 300, 800, 400)
        self.setWindowTitle("my first window")

        # Create FRAME_A
        self.FRAME_A = QtGui.QFrame(self)
        self.FRAME_A.setStyleSheet("QWidget { background-color: %s }" % QtGui.QColor(210,210,235,255).name())
        self.LAYOUT_A = QtGui.QGridLayout()
        self.FRAME_A.setLayout(self.LAYOUT_A)
        self.setCentralWidget(self.FRAME_A)

        # Place the matplotlib figure
        self.myFig = CustomFigCanvas()
        self.LAYOUT_A.addWidget(self.myFig, *(0,1))
        self.show()  

class CustomFigCanvas():
    def __init__(self):
        fig, ax = plt.subplots()
        line, = ax.plot([], [], lw=2)
        ax.grid()
        xdata, ydata = [], []
        plt.suptitle('TEST DATA', fontsize=20)
        plt.xlabel('TIME in seconds')
        plt.ylabel('FORCE in pounds')
        plt.show()

    def data_gen(t=0):
        cnt = 0
        while cnt < 2000:
            cnt += 1
            t += .2
            with serial.Serial('COM3', baudrate = 115200, timeout=1) as port:
                x = port.readline().replace(' lbf','')
            yield t, x
            print t, x

    def graph():

        ax.set_ylim(-20, 55000)
        ax.set_xlim(0, 10)
        del xdata[:]
        del ydata[:]
        line.set_data(xdata, ydata)
        return line,

    def run(data):
        # update the data
        t, y = data
        xdata.append(t)
        ydata.append(y)
        xmin, xmax = ax.get_xlim()

        if t >= xmax:
            ax.set_xlim(xmin, 2*xmax)
            ax.figure.canvas.draw()
        line.set_data(xdata, ydata)

        return line,


if __name__== '__main__':
    app = QtGui.QApplication(sys.argv)
    QtGui.QApplication.setStyle(QtGui.QStyleFactory.create('Plastique'))
    myGUI = CustomMainWindow()


    sys.exit(app.exec_())
J.Stahl
  • 51
  • 9
  • Is there any reason why you subclass `FigureCanvas` and `TimedAnimation` here? You can just create and call them from outside, which would make this a lot easier to understand for you. If you really need to subclass them both, you need to make sure that both are correctly initialized. It seems you're not using any of the classes properties, so renaming `data(self)` into `__init__(self)` might work. – ImportanceOfBeingErnest Apr 26 '17 at 18:26
  • In the closest project from what I wanted to accomplish that is how it was done, not being able to use the .plt when putting the plot in a canvas is killing me – J.Stahl Apr 26 '17 at 19:06
  • You can surely use pyplot, but that's not the point here. Can you link to the source where you got that code from? – ImportanceOfBeingErnest Apr 26 '17 at 19:15
  • I can do you one way better, I have the code that I have used and adapted and it works great stand-alone, It is from the matplotlib site decay example, here is the code. – J.Stahl Apr 26 '17 at 19:19
  • He used threading in his examples and that was a little to complicated for me so far, so I removed all of the extras when I was trying it. http://stackoverflow.com/questions/10944621/dynamically-updating-plot-in-matplotlib is the link – J.Stahl Apr 26 '17 at 19:27
  • It seems that the answer you refer to is indeed using `__init__` to properly initialize the class. – ImportanceOfBeingErnest Apr 26 '17 at 19:47
  • and I can still use the .plt in the figure canvas? – J.Stahl Apr 26 '17 at 19:58
  • Why shouldn't you?! – ImportanceOfBeingErnest Apr 26 '17 at 20:01
  • Here is what I changed my code for with the init class – J.Stahl Apr 27 '17 at 14:10

0 Answers0