将轴标签和标题添加到PyQtGraph ImageView

时间:2019-10-23 06:23:51

标签: python pyqt pyqtgraph

我使用PyQtGraph ImageView显示多维数据。

向这样的ImageView添加轴标签和标题的最简单方法是什么?

我尝试将LabelItem添加到基础ViewBox。我认为正确定位它需要破解基础布局。这是要走的路,还是有更简单的路?

import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg

app = QtGui.QApplication([])

# make ImageView with test data
imv = pg.ImageView()
data = np.fromfunction(lambda i, j: np.sin(i/16)*j/128, (512, 512), dtype=float) \
     + np.random.normal(scale=0.2, size=(512, 512))
imv.setImage(data)

# add label
vbox = imv.getView()
vbox.addItem(pg.LabelItem("this is a nice label"))

imv.show()
app.exec_()

PyQtGraph ImageView example

1 个答案:

答案 0 :(得分:1)

import numpy as np
from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph as pg

app = QtGui.QApplication([])

# Add Plot item to show axis labels
plot = pg.PlotItem()
plot.setLabel(axis='left', text='Y-axis')
plot.setLabel(axis='bottom', text='X-axis')

# make ImageView with test data
imv = pg.ImageView(view=plot)  # set the plot to ImageView's view  
data = np.fromfunction(lambda i, j: np.sin(i/16)*j/128, (512, 512), dtype=float) + np.random.normal(scale=0.2, size=(512, 512))
imv.setImage(data)

# add label
vbox = imv.getView()
vbox.addItem(pg.LabelItem("this is a nice label"))

imv.show()
app.exec_()

enter image description here