Grid lines below the outline in Matplotlib

I have one question about matplotlib grid lines. I'm not sure if this is possible or not. I draw the following graph as shown.

I will not give all the code, since it includes reading files.

However, the important part of the code here is

X, Y = np.meshgrid(smallX, smallY) Z = np.zeros((len(X),len(X[0]))) plt.contourf(X, Y, Z, levels, cmap=cm.gray_r, zorder = 1) plt.colorbar() ... # Set Border width zero [i.set_linewidth(0) for i in ax.spines.itervalues()] gridLineWidth=0.1 ax.set_axisbelow(False) gridlines = ax.get_xgridlines()+ax.get_ygridlines() #ax.set_axisbelow(True) plt.setp(gridlines, 'zorder', 5) ax.yaxis.grid(True, linewidth=gridLineWidth, linestyle='-', color='0.6') ax.xaxis.grid(False) ax.xaxis.set_ticks_position('none') ax.yaxis.set_ticks_position('none') 

enter image description here

Now my questions are:

If I put the grid lines below the path, they disappear as they are below it. If I put a grid line over the path, they look like they are now. However, what I would like to have is the grid lines, which should be visible, but should be below the black part of the outline. I am not sure if this is possible.

Thanks!

+4
source share
1 answer

In addition to specifying the z-order of the contours and grid lines, you can also try to mask the zero values โ€‹โ€‹of your contour data.

Here is a small example:

 import numpy as np import matplotlib.pyplot as plt x = np.arange(-2*np.pi, 2*np.pi, 0.1) y = np.arange(-2*np.pi, 2*np.pi, 0.1) X, Y = np.meshgrid(x, y) Z = np.sin(X) - np.cos(Y) Z = np.ma.masked_less(Z, 0) # you use mask_equal(yourData, yourMagicValue) fig, ax = plt.subplots() ax.contourf(Z, zorder=5, cmap=plt.cm.coolwarm) ax.xaxis.grid(True, zorder=0) ax.yaxis.grid(True, zorder=0) 

And the conclusion: enter image description here

+7
source

All Articles