Use an image as background for 2D data

2,069 views
Skip to first unread message

Simon Hafner

unread,
Dec 2, 2013, 2:15:53 AM12/2/13
to pyqt...@googlegroups.com
Hello

I'm looking for a way to use an image as background to plot data onto. The data consists of lines. With matplotlib, I achieved this effect using `imshow`. Is there a similar method in pyqtgraph? I considered stacking an ImageView onto a graph, but I haven't found how to stack widgets yet.

Cheers

Karl Bedrich

unread,
Dec 2, 2013, 4:43:24 AM12/2/13
to pyqt...@googlegroups.com
hey simon,

i use this code (shorted) to add an image to the top:


img = QtGui.QImage(filepath)#.scaled(self._img_res[0],self._img_res[1],
 
#transformMode=QtCore.Qt.SmoothTransformation).mirrored(vertical=True)
try:
 
#thats essential for pg.imageToArray(img)
 img
= img.convertToFormat(QtGui.QImage.Format_ARGB32_Premultiplied)
 imgArray
= pg.imageToArray(img, copy=True)
 
if not self._img:
 
self._img = pg.ImageItem(imgArray)
 
else:
 
self._img.setImage(imgArray)
 
#self._img.setRect(self._imgRect)
 
#self._img.setOpacity(self.pImageOpacity.value())
except AttributeError:
 
print "Image not valid!"
 
return False
#add the img to the viewbox of your plot:
self.display.view.vb.addItem(self._img)

bye,
karl

Luke Campagnola

unread,
Dec 2, 2013, 7:29:37 AM12/2/13
to pyqt...@googlegroups.com
On Mon, Dec 2, 2013 at 2:15 AM, Simon Hafner <react...@gmail.com> wrote:
Hello

I'm looking for a way to use an image as background to plot data onto. The data consists of lines. With matplotlib, I achieved this effect using `imshow`. Is there a similar method in pyqtgraph? I considered stacking an ImageView onto a graph, but I haven't found how to stack widgets yet.

You can add an ImageItem directly to a PlotItem or PlotWidget, then use ImageItem.scale, .translate, or .setRect to change the boundaries of the image relative to the plot data:

import pyqtgraph as pg

plt = pg.plot([1,5,2,4,3], pen='r')

img_data = pg.np.random.normal(size=(100,100))
img = pg.ImageItem(img_data)
plt.addItem(img)
img.setZValue(-100)  # make sure image is behind other data
img.setRect(pg.QtCore.QRectF(0, 0, 4, 5))
img.setLevels([0, 10])  # make image appear a bit darker

Karl's example is similar to this, and also shows how to load image data from a file.
Alternately, you could keep the ImageView and add a PlotDataItem to its internal ViewBox. 


Luke

Tom Jordan

unread,
Jul 19, 2016, 10:41:51 AM7/19/16
to pyqtgraph
Luke,
Thanks for providing a complete example. Your code works correctly. However, I would like to set a png image (it's a map) as the background of a plot.
When I run combine your code with Karl's to load a png from file, I get the exception below:

from pyqtgraph.Qt import QtGui
import pyqtgraph as pg

plt = pg.plot([1,5,2,4,3], pen='r')
img = QtGui.QImage('c:\Temp\solar-terminator-fig.png')
img = img.convertToFormat(QtGui.QImage.Format_ARGB32_Premultiplied)
imgArray = pg.imageToArray(img, copy=True)

imgitem = pg.ImageItem(imgArray)
plt.addItem(imgitem)
imgitem.setZValue(-100)  # make sure image is behind other data
imgitem.setRect(pg.QtCore.QRectF(0, 0, 4, 5))
imgitem.setLevels([0, 10])  # make image appear a bit darker

Traceback (most recent call last):
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 790, in rescaleData
    raise Exception('Weave is disabled; falling back to slower version.')
Exception: Weave is disabled; falling back to slower version.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 309, in paint
    self.render()
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 301, in render
    argb, alpha = fn.makeARGB(image.transpose((1, 0, 2)[:image.ndim]), lut=lut, levels=self.levels)
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 942, in makeARGB
    data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=int)
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 828, in rescaleData
    d2 *= scale
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'
Traceback (most recent call last):
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 790, in rescaleData
    raise Exception('Weave is disabled; falling back to slower version.')

Luke Campagnola

unread,
Jul 20, 2016, 10:25:23 AM7/20/16
to pyqt...@googlegroups.com
On Tue, Jul 19, 2016 at 7:41 AM, Tom Jordan <flacy...@gmail.com> wrote:

Traceback (most recent call last):
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 309, in paint
    self.render()
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\graphicsItems\ImageItem.py", line 301, in render
    argb, alpha = fn.makeARGB(image.transpose((1, 0, 2)[:image.ndim]), lut=lut, levels=self.levels)
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 942, in makeARGB
    data = rescaleData(data, scale/(maxVal-minVal), minVal, dtype=int)
  File "C:\apps\Anaconda3\lib\site-packages\pyqtgraph\functions.py", line 828, in rescaleData
    d2 *= scale
TypeError: Cannot cast ufunc multiply output from dtype('float64') to dtype('uint8') with casting rule 'same_kind'


There was a recent API change in numpy that causes this error. It is corrected in the develop branch on github. 
 
Reply all
Reply to author
Forward
0 new messages