1

I'm trying to plot a tiff image in pyqtgraph.

import numpy as np
import gdal
import pyqtgraph as pg
from PyQt4 import QtCore

gd = gdal.Open('myImage.tif')
data = np.array(gd.GetRasterBand(1).ReadAsArray())
pg.plot(data, title="my picture")

if __name__ == '__main__':
    import sys
    if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
        pg.QtGui.QApplication.exec_()

Im getting the error

Traceback (most recent call last):
  File "C:/Users/justin/PycharmProjects/pyqtgraph_examples/geotiff.py", line 18, in <module>
    pg.plot(data, title="my picture")
  File "C:\Python33\lib\site-packages\pyqtgraph\__init__.py", line 295, in plot
    w.plot(*args, **dataArgs)
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotItem\PlotItem.py", line 639, in plot
    item = PlotDataItem(*args, **kargs)
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 165, in __init__
    self.setData(*args, **kargs)
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 341, in setData
    dt = dataType(data)
  File "C:\Python33\lib\site-packages\pyqtgraph\graphicsItems\PlotDataItem.py", line 679, in dataType
    raise Exception('array shape must be (N,) or (N,2); got %s instead' % str(obj.shape))
Exception: array shape must be (N,) or (N,2); got (788, 744) instead

print(data.size) returns (788,744).

I'm thinking my numpy array is in the wrong form or I'm using the wrong pyqtgraph function but I'm not that familiar with either to know what to try next.

2
  • 3
    I've never used pyqtgraph before, but that error message makes it seem like it's expecting at most 2 rows of data (and so it's really for lines). The docs have a section called "Displaying images and video", which suggests pg.image(data) might work. Did you try that? Commented Jan 12, 2014 at 0:33
  • DSM: you are correct; pg.plot is only for plotting lines. You should write your comment in as an answer. Commented Jan 12, 2014 at 1:07

1 Answer 1

3

I think you want pyqtgraph.image. For example, here's a modifed version of your script (I have PySide installed):

import numpy as np
import pyqtgraph as pg
from PySide import QtCore
from scipy.ndimage import gaussian_filter


data = np.random.beta(0.5, 3, size=(500, 500))
data = gaussian_filter(data, sigma=(12, 3))

pg.image(data, title="my picture")

if __name__ == '__main__':
    import sys
    if sys.flags.interactive != 1 or not hasattr(QtCore, 'PYQT_VERSION'):
        pg.QtGui.QApplication.exec_()

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. My bad. When I was looking at the (really great) examples program for pyqtgraph, I was looking at the code showing the 'plot' function but was actually running a different image example which had me barking up the wrong tree. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.