绘制图像时如何将 Axis 置于最前面?
Posted
技术标签:
【中文标题】绘制图像时如何将 Axis 置于最前面?【英文标题】:How do you bring Axis to the front when plotting an image? 【发布时间】:2021-12-02 12:44:09 【问题描述】:如您所见,如果您运行以下代码,则缺少轴刻度。有谁知道如何用图像显示刻度?
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
view = plot.getViewBox()
view.setLimits(xMin = 0, xMax = 500, yMin = 0, yMax = 500)
plot.getAxis('left').()
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_()
【问题讨论】:
【参考方案1】:你需要让刻度出现在绘图上方,或者让它们向外。
for side in ('left', 'bottom'):
ax = plot.getAxis(side)
ax.setPen((60, 60, 60)) # change color of ticks
ax.setZValue(10) # make ticks appear above plot
ax.setStyle(tickLength=-5) # negative value to go inwards, positive value to go outwards
【讨论】:
以上是关于绘制图像时如何将 Axis 置于最前面?的主要内容,如果未能解决你的问题,请参考以下文章