Matplotlib help

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Matplotlib help

Binsha

Can I ask code help regarding matplotlib ?


_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Re: Matplotlib help

Paul Hobson-2
Yes. Be sure to include a small, but working example (that someone can copy/paste and run) that demonstrates the problem you are having along with a clear explanation of what you expect to happen.
-p

On Tue, Mar 19, 2019 at 7:34 PM Binsha S <[hidden email]> wrote:

Can I ask code help regarding matplotlib ?

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users
Reply | Threaded
Open this post in threaded view
|

Fwd: Matplotlib help

Binsha


---------- Forwarded message ---------
From: Binsha S <[hidden email]>
Date: Wed, Mar 20, 2019 at 1:16 PM
Subject: Re: [Matplotlib-users] Matplotlib help
To: Paul Hobson <[hidden email]>


Requirement Description
   
   I am trying to build a gui app for graph plotting. I used multiple files to read and plot as a slideshow. For that first set of data is read and plot then after a time interval it should plot next set of data and so on. I also have play ,pause, forward, backward for the slideshow for plot. Data from the file should be plotted as a slideshow if play button is clicked. For pause button, it should pause the slideshow. Forward and backward button for loop to plot forward play and backward play. 

I have a text file contains data as:

 -210.00      -10.13672      -10.10742      -10.01953
 -209.98       -1.93359       -1.93359       -1.93359
 -209.96       -5.36133       -5.33203       -5.33203
 ---
 300.02      -19.74609      -19.71680      -19.68750

What is not working

    Error occurs for the clicking of forward, backward, pause buttons.
    Multiple data plotting also not showing as a slideshow, one graph multiple data to be plotted and show as a slideshow

What I have tried:

graphgenerate.py
    from PyQt5 import QtCore, QtGui, QtWidgets
    class Ui_MplMainWindow(object):
        def setupUi(self, MplMainWindow):
            MplMainWindow.setObjectName("MplMainWindow")
            MplMainWindow.resize(1022, 662)
            self.centralwidget = QtWidgets.QWidget(MplMainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.upload_btn = QtWidgets.QPushButton(self.centralwidget)
            self.upload_btn.setGeometry(QtCore.QRect(80, 30, 91, 41))
            self.upload_btn.setObjectName("upload_btn")
            self.comboBox = QtWidgets.QComboBox(self.centralwidget)
            self.comboBox.setGeometry(QtCore.QRect(180, 40, 181, 22))
            self.comboBox.setStyleSheet("background-color: rgb(255, 255, 255);")
            self.comboBox.setObjectName("comboBox")
            self.graph_label = QtWidgets.QLabel(self.centralwidget)
            self.graph_label.setGeometry(QtCore.QRect(730, 40, 47, 16))
            self.graph_label.setObjectName("graph_label")
            self.backward_btn = QtWidgets.QPushButton(self.centralwidget)
            self.backward_btn.setGeometry(QtCore.QRect(550, 30, 41, 31))
            self.backward_btn.setStyleSheet("\n"
    "\n"
    "background-color: rgb(85, 85, 127);\n"
    "color: white;\n"
    "\n"
    "\n"
    "")
            self.backward_btn.setObjectName("backward_btn")
            self.playbutton = QtWidgets.QPushButton(self.centralwidget)
            self.playbutton.setGeometry(QtCore.QRect(590, 30, 41, 31))
            self.playbutton.setStyleSheet("\n"
    "background-color: rgb(255, 170, 0);\n"
    "\n"
    "\n"
    "")
            self.playbutton.setObjectName("playbutton")
            self.pause_button = QtWidgets.QPushButton(self.centralwidget)
            self.pause_button.setGeometry(QtCore.QRect(630, 30, 41, 31))
            self.pause_button.setStyleSheet("background-color: rgb(255, 170, 0);")
            self.pause_button.setObjectName("pause_button")
            self.forward_btn = QtWidgets.QPushButton(self.centralwidget)
            self.forward_btn.setGeometry(QtCore.QRect(670, 30, 41, 31))
            self.forward_btn.setStyleSheet("\n"
    "\n"
    "background-color: rgb(85, 85, 127);\n"
    "color: white;\n"
    "\n"
    "\n"
    "")
                self.forward_btn.setObjectName("forward_btn")
            self.time_interval_lbl = QtWidgets.QLabel(self.centralwidget)
            self.time_interval_lbl.setGeometry(QtCore.QRect(830, 40, 81, 16))
            self.time_interval_lbl.setObjectName("time_interval_lbl")
            self.timeinterval_spinbox = QtWidgets.QSpinBox(self.centralwidget)
            self.timeinterval_spinbox.setGeometry(QtCore.QRect(900, 30, 41, 31))
            self.timeinterval_spinbox.setProperty("value", 3)
            self.timeinterval_spinbox.setObjectName("timeinterval_spinbox")
            self.MplWidget = MplWidget(self.centralwidget)
            self.MplWidget.setGeometry(QtCore.QRect(40, 109, 1251, 511))
            self.MplWidget.setObjectName("MplWidget")
            self.column_label = QtWidgets.QLabel(self.centralwidget)
            self.column_label.setGeometry(QtCore.QRect(370, 40, 47, 13))
            self.column_label.setObjectName("column_label")
            self.column_Combobox = QtWidgets.QComboBox(self.centralwidget)
            self.column_Combobox.setGeometry(QtCore.QRect(420, 30, 91, 31))
            self.column_Combobox.setStyleSheet("background-color: rgb(255, 255, 255);")
            self.column_Combobox.setObjectName("column_Combobox")
            self.column_Combobox.addItem("")
            self.graphSpinbox = QtWidgets.QSpinBox(self.centralwidget)
            self.graphSpinbox.setGeometry(QtCore.QRect(770, 30, 42, 31))
            self.graphSpinbox.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
            self.graphSpinbox.setMinimum(5)
            self.graphSpinbox.setObjectName("graphSpinbox")
            self.label = QtWidgets.QLabel(self.centralwidget)
            self.label.setGeometry(QtCore.QRect(960, 40, 47, 13))
            self.label.setObjectName("label")
            self.comboBox_2 = QtWidgets.QComboBox(self.centralwidget)
            self.comboBox_2.setGeometry(QtCore.QRect(1010, 21, 69, 41))
            self.comboBox_2.setStyleSheet("background-color: rgb(255, 255, 255);\n"
    "color:green;\n"
    "")
            self.comboBox_2.setObjectName("comboBox_2")
            self.comboBox_2.addItem("")
            self.comboBox_2.addItem("")
            self.comboBox_2.addItem("")
            self.comboBox_2.addItem("")
            self.comboBox_2.addItem("")
            MplMainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MplMainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 1022, 21))
            self.menubar.setObjectName("menubar")
            MplMainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MplMainWindow)
            self.statusbar.setObjectName("statusbar")
            MplMainWindow.setStatusBar(self.statusbar)

            self.retranslateUi(MplMainWindow)
            QtCore.QMetaObject.connectSlotsByName(MplMainWindow)

        def retranslateUi(self, MplMainWindow):
            _translate = QtCore.QCoreApplication.translate
            MplMainWindow.setWindowTitle(_translate("MplMainWindow", "MainWindow"))
            self.upload_btn.setText(_translate("MplMainWindow", "Upload File"))
            self.graph_label.setText(_translate("MplMainWindow", "Graphs:"))
            self.backward_btn.setToolTip(_translate("MplMainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
    "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
    "p, li { white-space: pre-wrap; }\n"
    "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8pt; font-weight:400; font-style:normal;\">\n"
    "<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;color:black; \">Backward</p></body></html>"))
            self.backward_btn.setText(_translate("MplMainWindow", "<<"))
            self.playbutton.setToolTip(_translate("MplMainWindow", "<html><head/><body><p>Play </p></body></html>"))
            self.playbutton.setText(_translate("MplMainWindow", ">"))
            self.pause_button.setToolTip(_translate("MplMainWindow", "<html><head/><body><p>Pause</p></body></html>"))
            self.pause_button.setText(_translate("MplMainWindow", "||"))
            self.forward_btn.setToolTip(_translate("MplMainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
    "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
    "p, li { white-space: pre-wrap; }\n"
    "</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8pt; font-weight:400; font-style:normal;\">\n"
    "<p style=\" margin-top:12px; margin-bottom:12px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;color:black;\">Forward</p></body></html>"))
            self.forward_btn.setText(_translate("MplMainWindow", ">>"))
            self.time_interval_lbl.setText(_translate("MplMainWindow", "Time interval:"))
            self.column_label.setText(_translate("MplMainWindow", "Y Data:"))
            self.column_Combobox.setItemText(0, _translate("MplMainWindow", "<--Select--->"))
            self.label.setText(_translate("MplMainWindow", "X range:"))
            self.comboBox_2.setItemText(0, _translate("MplMainWindow", "120"))
            self.comboBox_2.setItemText(1, _translate("MplMainWindow", "40"))
            self.comboBox_2.setItemText(2, _translate("MplMainWindow", "80"))
            self.comboBox_2.setItemText(3, _translate("MplMainWindow", "20"))
            self.comboBox_2.setItemText(4, _translate("MplMainWindow", "240"))

    from mplwidget import MplWidget
    if __name__ == "__main__":
        import sys
        app = QtWidgets.QApplication(sys.argv)
        MplMainWindow = QtWidgets.QMainWindow()
        ui = Ui_MplMainWindow()
        ui.setupUi(MplMainWindow)
        MplMainWindow.show()
        sys.exit(app.exec_())

mplwidget.py

    from PyQt5 import QtWidgets, QtGui, QtCore
    from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
    from matplotlib.figure import Figure
    class MplWidget(QtWidgets.QWidget):
        def __init__(self, parent=None):
            QtWidgets.QWidget.__init__(self, parent)
            #self.canvas = FigureCanvas(Figure())
            self.fig=Figure()
            self.canvas=FigureCanvas(self.fig)
            vertical_layout = QtWidgets.QVBoxLayout()
            vertical_layout.addWidget(self.canvas)
            self.canvas.axes = self.canvas.figure.add_subplot(111)
            self.setLayout(vertical_layout)

graphmain.py
from asyncore import loop
    from PyQt5 import QtWidgets, QtCore, QtGui
    from PyQt5 import *
    from PyQt5.QtCore import QEventLoop, QTimer
    from PyQt5.QtWidgets import *
    from PyQt5.uic import loadUi
    from matplotlib.animation import FuncAnimation
    from matplotlib.backends.backend_qt5agg import (NavigationToolbar2QT as NavigationToolbar)
    import matplotlib.pyplot as plt
    import numpy as np
    from matplotlib import style
    import random
    import matplotlib.animation as animation
    import os
    import shutil
    import time

    class MatplotlibWidget(QMainWindow):
        def __init__(self):
            QMainWindow.__init__(self)
            loadUi("graphgenerate.ui", self)
            self.setWindowTitle("Graph Plotting App")
            self.upload_btn.clicked.connect(self.upload_File)
            self.playbutton.clicked.connect(self.selectFile)
            self.pause_button.clicked.connect(self.stopRun)
            self.backward_btn.clicked.connect(self.backward)
            self.forward_btn.clicked.connect(self.forward)
            self.timeinterval_spinbox.setValue(3)
            self.timeinterval_spinbox.setMinimum(1)
            self.addToolBar(NavigationToolbar(self.MplWidget.canvas, self))
            self.runs=True
            self.forwards=True
            self.i=0
            self.mini = 0
            self.maxi = 100
            global curr_pos
            curr_pos=0

        def upload_File(self):
            '''fileName,_= QFileDialog.getOpenFileName(None, "Open", "", "All Files (*)")
            shutil.copy(fileName,"Files")
            file=os.path.split(fileName)[1]
            self.comboBox.addItem(file)'''
            fname=[]
            file_dialog = QFileDialog()
            file_dialog.setFileMode(QFileDialog.ExistingFiles)
            file_dialog.setOption(QFileDialog.DontUseNativeDialog, True)
            file_view = file_dialog.findChild(QListView, 'listView')
            # to make it possible to select multiple directories:
            if file_view:
                file_view.setSelectionMode(QAbstractItemView.MultiSelection)
            f_tree_view = file_dialog.findChild(QTreeView)
            if f_tree_view:
                f_tree_view.setSelectionMode(QAbstractItemView.MultiSelection)
            if file_dialog.exec():
                paths = file_dialog.selectedFiles()
                for p in paths:
                    sp = os.path.split(p)[1]
                    fname.append(sp)
                    shutil.copy(p,"Files")
            self.comboBox.addItems(fname)

        def selectFile(self):

                QtWidgets.QMessageBox.warning(QtWidgets.QMessageBox(), 'Error', 'Please Select File and Column to plot')
                self.comboBox.activated[str].connect(self.selectColumn)


        def selectColumn(self,i):
            global plots,x_arr,y_arr
            path = "Files" + "\\" + i
            f1 = open(path, 'r')
            global data,m,n
            data = np.genfromtxt(f1)
            m = np.size(data, 0)
            n = np.size(data, 1)
            col = []
            for k in range(1,n):
                col.append("Column " + str(k+1))
            self.column_Combobox.clear()
            self.column_Combobox.addItems(col)
            selectedCol= self.column_Combobox.activated[str].connect(self.drawGraph)

        def drawGraph(self,i):
            col_value=int(i.split()[1])
            global  x,y,iters,val,gvalue
            x = data[:, 0].reshape(m, 1)
            y = data[:, col_value-1].reshape(m, 1)
            gvalue=self.graphSpinbox.value()
            iters=m //(gvalue-1)
            global current_iter
            current_iter = 0
            val=self.timeinterval_spinbox.value()
            self.plotData(iters,x,y,current_iter,val,gvalue)

        def plotDraw(self,x ,y):
            global x_data,y_data
            x_data=[]
            x_data.append(x)
            y_data=[]
            y_data.append(y)
            self.MplWidget.canvas.axes.clear()
            self.MplWidget.canvas.axes.plot(x, y)
            self.MplWidget.canvas.axes.legend(('cosinus', 'sinus'), loc='upper right')
            self.MplWidget.canvas.axes.set_title('Signal')
            self.MplWidget.canvas.draw()
            self.MplWidget.canvas.show()

        def plotData(self,iters,x,y,current_iter,val,gvalue):
             for i in range(gvalue):
                    self.plotDraw(x[current_iter:current_iter + iters], y[current_iter:current_iter + iters])
                    sec_value=str(val)+str(0)+str(0)+str(0)
                    current_iter = current_iter + iters
                    loop = QEventLoop()
                    QTimer.singleShot(int(sec_value), loop.quit)
                    loop.exec_()

        def stopRun(self,event=None):
            self.runs=False
            self.event.stop()

        def backward(self):
            curr_pos=len(x_data)
            if curr_pos!=0:
                self.plotData(iters, x_data[curr_pos], y_data[curr_pos], current_iter, val, gvalue)
                curr_pos = curr_pos - 1

        def forward(self):
            print("Forward button clicked")
            curr_pos=0
            if curr_pos<len(x_data):
                print(current_iter,val,gvalue)
                self.plotData(iters, x_data[curr_pos], y_data[curr_pos], current_iter, val, gvalue)
                curr_pos = curr_pos + 1

    if __name__ == "__main__":
        app = QApplication([])
        window = MatplotlibWidget()
        window.showMaximized()
        app.exec_()


Please help to fix this



On Wed, Mar 20, 2019 at 9:18 AM Paul Hobson <[hidden email]> wrote:
Yes. Be sure to include a small, but working example (that someone can copy/paste and run) that demonstrates the problem you are having along with a clear explanation of what you expect to happen.
-p

On Tue, Mar 19, 2019 at 7:34 PM Binsha S <[hidden email]> wrote:

Can I ask code help regarding matplotlib ?

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users

_______________________________________________
Matplotlib-users mailing list
[hidden email]
https://mail.python.org/mailman/listinfo/matplotlib-users