简体   繁体   English

在pyqtgraph中的图像上显示网格线

[英]Show grid lines over image in pyqtgraph

I'm drawing an image in pyqtgraph, and I'd like to be able to see the grid lines. 我正在用pyqtgraph绘制图像,我希望能够看到网格线。 But the grid lines are always drawn underneath the image, so any black areas of the image obscure the grid. 但是网格线总是绘制在图像下方,因此图像的任何黑色区域都会使网格模糊。 Here's a fairly minimal example: 这是一个相当小的例子:

import matplotlib  # necessary for interactive plots in pyqtgraph
import pyqtgraph as pg
import numpy as np
n = 100000
sigma_y = 1e-3
sigma_x = 1e-3
x0 = np.matrix([np.random.normal(0, sigma_x, n), np.random.normal(0, sigma_y, n)])
bins = 30
histogram, x_edges, y_edges = np.histogram2d(np.asarray(x0)[0], np.asarray(x0)[1], bins)
x_range = x_edges[-1] - x_edges[0]
y_range = y_edges[-1] - y_edges[0]

imv = pg.ImageView(view=pg.PlotItem())
imv.show()
imv.setPredefinedGradient('thermal')
imv.getView().showGrid(True, True)
imv.setImage(histogram, pos=(x_edges[0], y_edges[0]), scale=(x_range / bins, y_range / bins))

Here's what I see (after zooming out a little). 这是我看到的内容(缩小一点后)。 You can see that the black area of the image obscures the grid lines. 您会看到图像的黑色区域遮盖了网格线。

pyqtgraph图像截图

EDIT : it's possible in the GUI to change the black colour to transparent (not my first choice, but an OK workaround for now), so you can see the grid below the image. 编辑 :可以在GUI中将黑色更改为透明(这不是我的首选,但暂时可以解决),因此您可以看到图像下方的网格。 That works OK but I can't figure out how to do it in code. 可以,但是我不知道如何在代码中做到这一点。 How do I get the lookup table out of the ImageView to modify it? 如何从ImageView获取查找表进行修改?

Here is what I did. 这是我所做的。

glw = pyqtgraph.GraphicsLayoutWidget()
pw = glw.addPlot(0, 0)

# Fix Axes ticks and grid
for key in pw.axes:
    ax = pw.getAxis(key)

    # Set the grid opacity
    if grid_is_visible:
        ax.setGrid(grid_opacity * 255)
    else:
        ax.setGrid(False)

    # Fix Z value making the grid on top of the image
    ax.setZValue(1)

This did cause some other issue, I think. 我认为这确实引起了其他问题。 It may have been the context menu or it had to do with panning and zooming, because of how Qt was signaling the events. 由于Qt表示事件的方式,它可能是上下文菜单,或者与平移和缩放有关。 One axes got the event priority and prevented the event from propagating for the other axes to pan and zoom. 一个轴具有事件优先级,并阻止事件传播以使其他轴平移和缩放。 I submitted a pull request to pyqtgraph, so that might not be an issue anymore. 我向pyqtgraph提交了拉取请求,因此可能不再是问题。 I can't remember what caused the problem though. 我不记得是什么引起了问题。 It may work just fine for you. 它可能对您来说很好。 I was doing a lot of other things like changing the background color and changing the viewbox background color which caused some small issues. 我做了很多其他事情,例如更改背景色和更改视图框背景色,这导致了一些小问题。

As a note I also changed the image z value. 注意,我还更改了图像z值。 You shouldn't have to though. 您不必如此。

imv.setZValue(1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM