Matplotlib - How to overlay a grid on a different axes over an image?
I have attempted to overlay a grid that is on a separate axes over an image that is loaded into matplotlib using imread. The reason for the use of a separate axis is to display grid lines and detect mouse clicks using a开发者_运维知识库 different coordinate system rather than the default one created by matplotlib when the image is loaded. Changing the zorder of the grid axis to a higher value than the image axis works, but then the image can't be seen. Is there any other method?
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()
You can set the background of your grid to be transparent with the set_alpha
command, similar to the answer to the question at How to set opacity of background colour of graph wit Matplotlib
ax1 = fig.add_subplot(111)
ax1.grid()
ax1.patch.set_alpha(0)
精彩评论