How to change the y axis scale in ImageView

360 views
Skip to first unread message

S Martin

unread,
May 2, 2018, 4:53:37 PM5/2/18
to pyqtgraph
Hello,

I am trying to use ImageView to display an image but with axis included and specified y range.  Basically I'm doing something like this:

plt = pg.PlotItem(labels={'bottom':('time',''),'left':('frequency','')})
imv = pg.ImageView(view=plt)
x0, x1 = (0, 500)
y0, y1 = (0, 16)
xscale = (x1-x0)/img.shape[0]
yscale = (y1-y0)/img.shape[1]
imv.setImage(img, pos=[x0,y0], scale=[xscale, yscale])
plt.setAspectLocked(False)

Now the xaxis runs 0 to 500 left to right, but the yaxis runs 0 to 16 top to bottom.  However, I want the yaxis to start at the bottom at 0 and run to 16 at the top (a usual increasing y axis).  I am not sure how to set the axis range within ImageView.

Thanks,
Steve

Patrick

unread,
May 2, 2018, 11:20:59 PM5/2/18
to pyqtgraph

Hi,


This is some simplified parts from my code that I think does what you describe:

# Set up the plot and add the ImageItem
self.plot = self.plot_graphicsLayoutWidget.addPlot()
self.plot.showGrid(x=True, y=True)
self.plot.setLabels(bottom="Wavelength (nm)")
self.image = pg.ImageItem(np.zeros((1, 1)))
self.plot.addItem(self.image)
#...
# Acquire image from somewhere and set the ImageItem to use it
self.image.setImage(image)
# x-axis coordinate values are wavelengths (eg, 450--750)
# y-axis coordinates are simply the camera pixel numbers (0--199)
self.xlabels = np.arange(450, 750)
# Translate and scale the ImageItem to match the x-axis labels
self.image.translate(self.xlabels[0], 0)
self.image.scale((self.xlabels[-1] - self.xlabels[0])/len(self.xlabels), 1)





Patrick

S Martin

unread,
May 3, 2018, 9:30:14 AM5/3/18
to pyqtgraph
Thanks Patrick.

Your code certainly helps with over plotting an image on a plot region, but I am looking to use ImageView for its built-in functionality (ROI, histogram, etc...).  So, I'd like to reverse the y-axis labels within ImageView.

Luke Campagnola

unread,
May 3, 2018, 12:10:27 PM5/3/18
to pyqt...@googlegroups.com
You can use `PlotItem.invertY(False)` to set the direction of the y axis.
You might also need to use the `axes` argument to `ImageView.setImage()`, depending on the order of axes in your image data.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyqtgraph/96aa41d8-a1cd-45dd-ba55-c58e8eba265d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

S Martin

unread,
May 3, 2018, 2:24:50 PM5/3/18
to pyqtgraph
Ah, yes, PlotImage.invertY(False) worked!

Thanks!
Steve


On Thursday, May 3, 2018 at 12:10:27 PM UTC-4, Luke Campagnola wrote:
You can use `PlotItem.invertY(False)` to set the direction of the y axis.
You might also need to use the `axes` argument to `ImageView.setImage()`, depending on the order of axes in your image data.
On Wed, May 2, 2018 at 1:53 PM, S Martin <smar...@gmail.com> wrote:
Hello,

I am trying to use ImageView to display an image but with axis included and specified y range.  Basically I'm doing something like this:

plt = pg.PlotItem(labels={'bottom':('time',''),'left':('frequency','')})
imv = pg.ImageView(view=plt)
x0, x1 = (0, 500)
y0, y1 = (0, 16)
xscale = (x1-x0)/img.shape[0]
yscale = (y1-y0)/img.shape[1]
imv.setImage(img, pos=[x0,y0], scale=[xscale, yscale])
plt.setAspectLocked(False)

Now the xaxis runs 0 to 500 left to right, but the yaxis runs 0 to 16 top to bottom.  However, I want the yaxis to start at the bottom at 0 and run to 16 at the top (a usual increasing y axis).  I am not sure how to set the axis range within ImageView.

Thanks,
Steve

--
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.
Reply all
Reply to author
Forward
0 new messages