Plotting and removing graphs with pyqtgraph

2,274 views
Skip to first unread message

Guillaume Gotthard

unread,
Nov 4, 2014, 9:54:33 AM11/4/14
to pyqt...@googlegroups.com

Hi everybody,

I am curently trying to develop a python based software for reading and manipulating spectroscopy results.
I have written a python script and a Gui, principally based on pyqtgraph widget implemented in PyQt4.

My script allows me to load files from a browser (in a treeView widget), put their names in a listView widget (each items have a checkbox that is checked by default) and at the same time plot this checked item in the pyqtgraph widget.

I show here a view of my gui.



As you can see, I am able to plot spectra each times I load a new spectrum.
However, I am not able to remove each plot separately. I can remove the last plotted spectrum, but not each of them separately.

Since I am a begginer in python scripting, I cannot identify where is my error, and how to fix the code...
I suspect the problem stems from my way to plot and remove spectrum in association with the checkbox status of the listview.

Following are the relevant code parts:




@QtCore.pyqtSlot(QtCore.QModelIndex)
def on_treeview_clicked(index):
   
global clicked_spectrum
   
global alldata
   
global spectra_filenames
   
global spectra_data
   
global fileName
   
global filePath
   
global item

    indexItem
= model.index(index.row(), 0, index.parent())

   
# path or filename selected
    fileName
= model.fileName(indexItem)
    fileName
= str(fileName)
   
# full path/filename selected
    filePath
= model.filePath(indexItem)
    filePath
= str(filePath)
   
   
# if len(clicked_spectrum) == 0:
   
#    clicked_spectrum = [(fileName), (filePath)]
   
# else:
    clicked_spectrum
.append([(fileName), (filePath)])
    spectra_filenames
[filePath] = fileName
   

   
    data
= np.genfromtxt(filePath, skip_header=18, skip_footer=1, usecols=(0, 1), autostrip=True)
    alldata
.append(data)
   
print "number of \"good\" spectra:", len(alldata)

    spectra_data
[filePath] = data

    item
= QtGui.QStandardItem(fileName)
   
#item = QtGui.QCheckBox(fileName)

   
# add a checkbox to it
    item
.setCheckable(True)
    item
.setCheckState(2)

    on_item_changed
(item)
     
   
# Add the item to the model
    model_list
.appendRow(item)

       
def on_item_changed(item):
   
global filePath
   
    i
= 0
   
for searchname in clicked_spectrum:
       
# print searchname
       
if searchname[0] == item.text():
           
#print "found!!", searchname
           
break
       
else:
            i
= i+1

   
# print "keys:", spectra_filenames.keys()
   
for searchname in spectra_filenames.keys():
       
# print searchname , spectra_filenames[searchname]
       
if spectra_filenames[searchname] == item.text():
           
#print "found!!", searchname
            foundname
= searchname
           
break

    spectrum_data
= spectra_data[foundname]

   
for searchname in spectra_filenames.keys():
       
if item.checkState() == QtCore.Qt.Checked:

            filePath
= pg.PlotDataItem(spectra_data[filePath], name=item.text(), pen={'color': (255,0,0), 'width':p.param('Graphics Parameters').param('Line_width').value(), 'style':p.param('Graphics Parameters').param('Line_style').value()})
            ui
.graphicsView.addItem(filePath, clickable=True)

           
break

       
else:

            ui
.graphicsView.removeItem(filePath)
           
break



##############################
#####    Main Program    #####
##############################

###Browsing widget####

clicked_spectrum  
= []
alldata          
= []
spectra_filenames
= {}
spectra_data      
= {}

if os.name == 'nt': # Windows
    basepath
= 'C:\\Users\\'
else:
    basepath
= '.'   # current directory

model
= QtGui.QFileSystemModel()

filters
= QStringList("*.txt")
model
.setNameFilters(filters) # allows to "grey" files that have not the right extension
#model.setNameFilterDisables(0) #allows to completely hide files that have not the right extension
root
= model.setRootPath(basepath)
ui
.files_treeView.setModel(model)
ui
.files_treeView.setSelectionMode(QTreeView.ExtendedSelection) # allows multiple file selection for loading
ui
.files_treeView.setRootIndex(root)
ui
.files_treeView.setHeaderHidden(1)
ui
.files_treeView.hideColumn(1)
ui
.files_treeView.hideColumn(2)
ui
.files_treeView.hideColumn(3)


ui
.files_treeView.doubleClicked.connect(on_treeview_clicked)

###List of loaded files####
 
list
= QtGui.QListView()

model_list
= QtGui.QStandardItemModel(list)
ui
.listView.setSelectionMode(QAbstractItemView.ExtendedSelection) # allows multiple file selection for unloading



ui
.listView.setModel(model_list)

model_list
.itemChanged.connect(on_item_changed)


# print model_list.itemFromIndex(QtGui.QModelIndex(2)).text()

#########################
###Plotting of Graphs####
#########################


###Setting graphs rendering####

ui
.graphicsView.setLabel('left', text='Absorption [OD]')
ui
.graphicsView.setLabel('bottom', text='Wavelength [nm]')







Please, if anyone has an idea, I would be very grateful.

Cheers,

Guillaume

Mathew Schwartz

unread,
Nov 4, 2014, 6:47:58 PM11/4/14
to pyqt...@googlegroups.com
It looks like your using a global variable for the items in your graph, filepath.  I suspect you are overwriting your items each time you load a new one, so the only one that is actually stored is the last one.  You should try avoid globals, but if you dont want to, you can try making a global array or dictionary.  Each time you load a file, you append your new plotitem to that global.  

--
You received this message because you are subscribed to the Google Groups "pyqtgraph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyqtgraph+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/42826fb7-1598-4ed8-b7c2-341b81803930%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages