在 pyqtgraph 中将图像拉伸到窗口大小

问题描述 投票:0回答:2

我目前正在尝试创建一个 PyQtGraph gui 以在新数据传入时使用类似于此的代码重复绘制图像:

self.app = QtGui.QApplication([])
self.win = QtGui.QMainWindow()
self.win.resize(800, 600)
self.imv = pg.ImageView()
self.win.setCentralWidget(self.imv)
self.win.setWindowTitle('My Title')
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.check_for_new_data_and_replot)
self.timer.start(100)
self.win.show()

然后每次我获得新数据时,我都会绘制图像:

self.imv.setImage(self.data_array)

我遇到的一个问题是我的数据数组通常有一个倾斜的纵横比,即它通常真的是“又高又瘦”或“又矮又胖”,并且绘制的图像具有相同的比例。

有没有办法拉伸图像以适合窗口?我查看了 ImageViewImageItem 的文档,但找不到我需要的东西。 (也许它在那里,但我无法识别它。)

qt pyqt4 pyqtgraph
2个回答
2
投票

我想通了——使用较低级别的 ImageItem 类以拉伸以适合窗口大小的方式显示图像:

self.app = QtGui.QApplication([])
self.win = pg.GraphicsLayoutWidget()
self.win.resize(800, 600)
self.img = pg.ImageItem()
self.plot = self.win.addPlot()
self.plot.addItem(self.img)
self.win.setWindowTitle('My Title')
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.check_for_new_data_and_replot)
self.timer.start(100)
self.win.show()

并更新图像数据:

self.img.setImage(self.data_array)

这也让我可以在侧面显示轴刻度,这也是我想要的功能。


0
投票

如果您仍想保留完整的 ImageView 功能,您可以将 ImageView 嵌入到 PlotItem 中:

self.plot = pg.PlotItem()
self.imv = pg.ImageView(view=self.plot)

这也会给你轴刻度。然后你可以使用

self.plot.setAspectLocked(False)

拉伸图像以适合窗口大小。

© www.soinside.com 2019 - 2024. All rights reserved.