Matplotlib - 如何在图像上的不同轴上叠加网格?

mr_*_*_js 2 grid image matplotlib

我尝试将位于单独轴上的网格覆盖在使用 imread 加载到 matplotlib 中的图像上。使用单独轴的原因是使用不同的坐标系显示网格线并检测鼠标单击,而不是加载图像时 matplotlib 创建的默认坐标系。将网格轴的 zorder 更改为比图像轴更高的值是可行的,但随后看不到图像。还有其他方法吗?

ax1 = fig.add_subplot(111)
ax1.grid()
ax2 = ax1.twinx()
im = matplotlib.image.imread('pic.png')
ax2.imshow(im)
ax1.set_zorder(1) #grid is shown but image disappears
draw()
Run Code Online (Sandbox Code Playgroud)

Ste*_*rry 5

您可以使用该命令将网格的背景设置为透明set_alpha,类似于如何设置 Matplotlib 图形背景颜色的不透明度问题的答案

ax1 = fig.add_subplot(111)
ax1.grid()
ax1.patch.set_alpha(0)
Run Code Online (Sandbox Code Playgroud)