How to plot positive and negative Y values in ImageItem

160 views
Skip to first unread message

alxcpa 01101

unread,
May 24, 2019, 10:26:59 AM5/24/19
to pyqtgraph
I'm having trouble getting positive and negative Y values for a spectrogram.  I can use img.scale(dt,df) to scale properly but I would actually like to scale and shift down by the half of the max frequency, like img.scale(dt, df  (then - fmax/2)).  I can't seem to find the source for .scale() or .setPos(), in ImageView it references the functions to ImageItem, but it is not in the ImageItem source.  My only other idea is to create 2 images and scale one positive and one negative and combine them, but this doesn't seem necessary.  Any ideas?

Patrick

unread,
May 26, 2019, 9:02:40 PM5/26/19
to pyqtgraph
Hi,

ImageItem is a descendent of Qt QGraphicsItem, so the real work happens down there somewhere. Here's a an example of how I do what you describe:

# x, y are arrays of x and y axis labels, assumes regularly spaced grid
# imageItem has been added to the plotItem

x_scale
= (self.x[-1] - self.x[0])/(self.x.shape[0]-1) if self.x.shape[0] > 1 else 1.0
y_scale
= (self.y[-1] - self.y[0])/(self.y.shape[0]-1) if self.y.shape[0] > 1 else 1.0

self.plotItem.setLimits(xMin=self.x[0] - x_scale/2, xMax=self.x[-1] + x_scale/2,
                        yMin
=self.y[0] - y_scale/2, yMax=self.y[-1] + y_scale/2)

self.imageItem.resetTransform()
self.imageItem.scale(x_scale, y_scale)
self.imageItem.setPos(self.x[0] - x_scale/2, self.y[0] - y_scale/2)

Patrick

alxcpa 01101

unread,
May 27, 2019, 9:32:56 AM5/27/19
to pyqtgraph
Thanks Patrick, this worked for me.
Reply all
Reply to author
Forward
0 new messages