Adding multiple plots with data from QListWidget

238 views
Skip to first unread message

Arthur Pijpaert

unread,
Aug 10, 2016, 10:27:46 AM8/10/16
to pyqtgraph

Hallo,

I want to add plots with data from a listwidget after clicking on the item on the list. So I first import a text file that gets displayed in the list. Now I want to add a plot of a function to the graph so that you can add multiple plots to 1 figure. 

I thought using a dictionary where you store the functions you want to plot in one figure, but I dont know how you can read out the dictionary if the values are arrays.

I use python 2.7 with PyQtGraph

This is my script:
from PyQt4.uic import loadUiType
from PyQt4.Qt import*

from pyqtgraph.Qt import QtGui, QtCore
import numpy as np
import pyqtgraph as pg


Ui_MainWindow, QMainWindow = loadUiType('window.ui')

class Main(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super(Main, self).__init__()
        self.setupUi(self)
        self.fig_dict = {}
        self.plot_dict = {}   
        
        print self.fig_dict.values()
        
        pw = pg.PlotWidget(name='Plot')
        self.mplVl.addWidget(pw)
        xd = np.arange(10)
        yd = np.arange(10)
        p1 = pw.plot()
        p1.setData(y=yd, x=xd)
        p1.setPen((200,200,100))
       
        
        self.mplFigs.itemClicked.connect(self.clicked_fig)
        self.mplFigs.setSelectionMode(QtGui.QAbstractItemView.ExtendedSelection)
        
    
        '''
        x1 = np.arange(10), 
        y1 = np.sin(x1)     
        plot = pw.plot()
        plot.setData(x=x1, y=y1)
        plot.setPen((200,200,100))  
        pw.addItem(plot)'''


        
        openFile = QtGui.QAction('&Open File', self)
        openFile.setShortcut('Ctrl+O')
        openFile.setStatusTip('Open File')
        openFile.triggered.connect(self.open_file)
        
        mainMenu = self.menuBar()
        fileMenu = mainMenu.addMenu('&File')
        fileMenu.addAction(openFile)


    
    def open_file(self):
     
        self.open = QtGui.QFileDialog.getOpenFileNames(self, 'Select the files of one type of measurement')
        n=1
        b=0
        fig=0
        
        for path in self.open:
            file = open(path, 'r')
            text = np.genfromtxt(file)
            x = text[:,0]            
            a = text[:,1] 
            b = b + a
            y = b / n
            n=n+1
        
        fig = np.column_stack((x, y))  
#        print fig  
#        fig1 = Figure()
#        ax1f1 = fig1.add_subplot(111)
#        ax1f1.plot(x, y)
        self.add_fig(raw_input('name: '), fig)
        
        
    def clicked_fig(self):
        return True

        

        

    def change_fig(self, item):
        text = item.text()
        self.rm_mpl()
        self.add_mpl(self.fig_dict[text])    

        
    def add_fig(self, name, fig):
        self.fig_dict[name] = fig
        self.mplFigs.addItem(name)
        
        
    def add_mpl(self, fig):
        self.canvas = FigureCanvas(fig)
        self.mplVl.addWidget(self.canvas)
        self.canvas.draw()
        
        self.toolbar = NavigationToolbar(self.canvas,
                                         self.mplWindow,
                                         coordinates=True)
        self.mplVl.addWidget(self.toolbar)

        
    def rm_mpl(self):
        self.mplVl.removeWidget(self.canvas)
        self.canvas.close()
        self.mplVl.removeWidget(self.toolbar)
        self.toolbar.close()        
        
        

if __name__ == '__main__':
    import sys
    from PyQt4 import QtGui
    
    app = QtGui.QApplication(sys.argv)
    main = Main()
    main.show()
    sys.exit(app.exec_())


I also added the .ui in the attechments. Every tip is welkom!
window.ui

fscl...@gmail.com

unread,
Aug 15, 2016, 10:03:52 AM8/15/16
to pyqtgraph
Hi,

What I am doing is to use the indexes of the QlistWidget box:

for ListIndex in QListWidget
     idx=ListIndex.row()

idx is an index (you need to create the proper one) to go through the data stored in the variable which is the feed for my QLIstView

Then you can loop using your index through your data and plot them just including the .plot(x,y[idx]) in the loop
Reply all
Reply to author
Forward
0 new messages