当我更改图像轴的比例时,我的ScaleBar显示不正确的比例。更改轴时如何更新比例尺?
from PyQt5 import QtWidgets
import pyqtgraph as pg
import numpy as np
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
imvOCTTopLeft = pg.ImageView(view=pg.PlotItem())
imvOCTTopLeft.setImage(np.random.normal(size=(100,100)))
imvOCTTopLeft.view.getAxis('left').setScale(0.6)
imvOCTTopLeft.view.getAxis('bottom').setScale(0.4)
scale = pg.ScaleBar(size=10,suffix = "px")
viewbox = imvOCTTopLeft.view
if not isinstance(viewbox, pg.ViewBox): viewbox = viewbox.getViewBox()
scale.setParentItem(viewbox)
scale.anchor((1, 1), (1, 1), offset=(-20, -20))
imvOCTTopLeft.show()
sys.exit(app.exec_())
此图像显示比例尺显示大约4个像素,但指出它显示10个像素。
我认为这是因为我更改了轴刻度。
答案 0 :(得分:0)
这似乎是一个错误:link。发出sigRangeChanged
后,视图框会重新缩放。
“ Hacky”解决方案是延迟ScaleBar
更新:
(您可能需要尝试time
,100
和10
为我工作。如果不起作用,请增加它。)
from PyQt5 import QtWidgets, QtCore
import pyqtgraph as pg
import numpy as np
def updateDelay(scale, time):
QtCore.QTimer.singleShot(time, scale.updateBar)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
plotItem = pg.PlotItem()
imvOCTTopLeft = pg.ImageView(view=plotItem)
imvOCTTopLeft.setImage(np.random.normal(size=(100, 100)))
imvOCTTopLeft.view.getAxis('left').setScale(0.6)
scale = 0.4 #edit
imvOCTTopLeft.view.getAxis('bottom').setScale(scale) #edit
scale = pg.ScaleBar(size=10*(1/scale), suffix="px") #edit
scale.text.setText('10 px') #edit
plotItem.sigRangeChanged.connect(lambda: updateDelay(scale, 10)) # here: time=10ms
viewbox = imvOCTTopLeft.view
if not isinstance(viewbox, pg.ViewBox): viewbox = viewbox.getViewBox()
scale.setParentItem(viewbox)
scale.anchor((1, 1), (1, 1), offset=(-20, -20))
imvOCTTopLeft.show()
updateDelay(scale, 100) # here time=100ms
sys.exit(app.exec_())