pyqtgraph对带有ImageItem的轴使用任意值

2024-05-01 21:29:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一种感觉,我在这里遗漏了一些明显的东西,但我在互联网上找不到任何有用的东西

我有一个应用程序,它使用pyqtgraph显示信号。该信号有1000个样本,代表0-0.5范围内的值。 用正确的X轴值绘制它很简单-我只是用正确的元素范围和数量制作一个linspace,并将其用作plot中的x

但是如何对ImageItem内的PlotWidget执行相同的操作呢?它使用元素的数量作为它的X轴,而我不知道如何告诉它使用正确的值

看一看屏幕截图——我需要对图像使用与下图相同的X轴值。 application screenshot

对于相关的代码位:
首先,我创建我的X值:

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]))

对于绘图和图像的更新:

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)

因此,问题归结为如何将我的xvalImageItem(或包含ImageItemPlotWidget)一起使用

非常感谢你的帮助


Tags: 图像selfnumpy元素数量len信号plot
1条回答
网友
1楼 · 发布于 2024-05-01 21:29:19

您可以使用“setRect”定义图像坐标,并传递“QRectF(x,y,width,height)”(x,y表示左上角或左下角,如果启用了pg.setConfigOptions(imageAxisOrder=“row major”):

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))

相关问题 更多 >