ImageView within GraphicsLayou

54 views
Skip to first unread message

Gregory Bättig

unread,
Sep 13, 2018, 6:03:36 AM9/13/18
to pyqtgraph

I have a QT application where I use the GraphicsView in combination with GraphicsLayout. I took the example "GraphicsLayout.py" from the PyQtGraph repository as a starting point. The below code snippet shows from where I started out.

app = QtGui.QApplication([])
view
= pg.GraphicsView()
l
= pg.GraphicsLayout(border=(100,100,100))
view
.setCentralItem(l)
view
.show()
view
.setWindowTitle('pyqtgraph example: GraphicsLayout')
view
.resize(800,600)

p1
= l.addPlot(title="Plot 1")
p2
= l.addPlot(title="Plot 2")
vb
= l.addViewBox(lockAspect=True)
img
= pg.ImageItem(np.random.normal(size=(100,100)))
vb
.addItem(img)

This approach works fine but I now would like to add an ImageView instead of an ImageItem to the layout. Mainly because I want to take advantage of the histogram and slider functionalities already inbuilt in ImageView objects.

In the example "DataSlicing" they add ImageView objects to a layout, but there it is a QtGui.QtGridLayout object and the ImageViews are added as widgets and not as items.

cw = QtGui.QWidget()
win
.setCentralWidget(cw)
l
= QtGui.QGridLayout()
cw
.setLayout(l)
imv1
= pg.ImageView()
imv2
= pg.ImageView()
l
.addWidget(imv1, 0, 0)
l
.addWidget(imv2, 1, 0)

Is there a way to combine these two approaches, by adding ImageView objects to a pg.GraphicsLayout, or is that not the right way to go?

Thanks for any kind of advice

Patrick

unread,
Sep 14, 2018, 12:56:26 AM9/14/18
to pyqtgraph
Hi,

Yeah, you won't be able to do that as ImageView is a widget and not a GraphicsItem. You could copy the ImageView code and modify it to do what you want, but might be overkill. If you just want the histogram and gradient editor, then something like this would do it:

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

app = QtGui.QApplication([])
view = pg.GraphicsView()
l = pg.GraphicsLayout(border=(100,100,100))
view.setCentralItem(l)
view.show()
view.setWindowTitle('pyqtgraph example: GraphicsLayout')
view.resize(800,600)

p1 = l.addPlot(title="Plot 1")
p2 = l.addPlot(title="Plot 2")
vb = l.addViewBox(lockAspect=True)
img = pg.ImageItem(np.random.normal(size=(100,100)))
vb.addItem(img)
cbar = pg.HistogramLUTItem(image=img)
cbar.gradient.loadPreset('thermal')
l.addItem(cbar)

app.exec_()


If you wanted the timeline/slider too, then you'd need to replicate it as well, which wouldn't be too hard. Look inside the ImageView code and copy/paste a bit.

Patrick

Gregory Bättig

unread,
Sep 14, 2018, 2:47:34 AM9/14/18
to pyqtgraph
Thanks Patrick! I will try out your proposed solution.
Have a nice weekend!
Reply all
Reply to author
Forward
0 new messages