0

I have a feeling that I'm missing something obvious here, but I didn't find anything useful on the internet.

I have an application that displays a signal using pyqtgraph. The signal has 1000 samples representing values in 0 - 0.5 range. Plotting it with correct X-axis values is simple - I'm just making a linspace with correct range and number of elements and use it as x in plot.

But how to do the same for the ImageItem that is inside a PlotWidget? It uses number of elements as its X-axis, and I wasn't able to figure out how to tell it to use correct values.

Take a look at the screenshot - I need to use the same X-axis values for images as for the plots below. application screenshot

For the relevant bits of code:
First, I create my X-values:

self.xval_h = numpy.linspace(0, 0.5, len(self.spectrum_h[-1]))
self.xval_v = numpy.linspace(0, 0.5, len(self.spectrum_v[-1]))

And for the update of the plots and images:

def update_spectrum(self):
    self.HSpectrum.plot(self.xval_h, self.spectrum_h[-1], clear=True)
    self.VSpectrum.plot(self.xval_v, self.spectrum_v[-1], clear=True)

def update_trace(self):
    self.image_h.setImage(numpy.array(self.spectrum_h).T)
    self.image_v.setImage(numpy.array(self.spectrum_v).T)

So, the question boils down to how do I use my xvals with ImageItem (or PlotWidget that contains the ImageItem)?

Many thanks for help!

daneos
  • 23
  • 5
  • Maybe, this answer (https://stackoverflow.com/a/41963986/11827059) will help you. – ENIAC Aug 27 '20 at 17:07
  • I found and tried that. It works in principle, but it generates ticks exactly as I specify them. So for full range I get a tick every one value, if I decrease number of ticks, then the same ticks are applied no matter what the zoom level is. This means I would need to manually adjust tick density to the zoom level. What I'm looking for is a nice way to map 0-1000 range into 0-0.5 range with default tick behaviour. – daneos Aug 27 '20 at 17:24

1 Answers1

0

You can define the coordinates of your image with "setRect" and passing "QRectF(x,y,width,height)" (x,y for the top-left corner or bottom-left if pg.setConfigOptions(imageAxisOrder="row-major") is enabled):

image_width = abs(self.xval_h[0]-self.xval_h[0])
image_height = abs(self.xval_h[0]-self.xval_h[0])  # if x and y-scales are the same
pixel_size = image_width/(self.xval_h.size-1)
self.image_h.setRect(QRectF(self.xval_h[0]-pixel_size/2, self.xval_h[0]-pixel_size/2, image_width, image_height))
DCCoder
  • 1,587
  • 4
  • 16
  • 29
yuzak
  • 1
  • 2