How to plot the grid line only using pcolor/pcolor

2019-05-12 03:40发布

问题:

By using ax.axhline & ax.axvline I can plot the grid network shows like this:

http://i4.tietuku.com/e90652c9a56b5e61.png

the code are reproduced here:

 lon_grid  = np.linspace(113.5,115.49,36)
 lat_grid  = np.linspace(37.40,38.78,30)

 lon_grid,lat_grid = np.linspace(xc1,xc2,36), np.linspace(yc1,yc2,30)
 for i in range(0,len(lon_grid),1):
     ax.axvline(x=lon_grid[i], linestyle='-', color='black', linewidth=0.75,    zorder=3)
 for i in range(0,len(lat_grid),1):
     ax.axhline(y=lat_grid[i], linestyle='-', color='black', linewidth=0.75, zorder=3)         

Here is my question

Can I use pcolor/pcolrmesh to just draw the outline of the grid

回答1:

I would use ax.grid for this instead. To control the positions of the grid lines you could set the positions of the ticks, e.g.:

fig, ax = plt.subplots(1, 1)

ax.grid(True, which='minor', axis='both', linestyle='-', color='k')

ax.set_xticks(lon_grid, minor=True)
ax.set_yticks(lat_grid, minor=True)

ax.set_xlim(lon_grid[0], lon_grid[-1])
ax.set_ylim(lat_grid[0], lat_grid[-1])


If you really want to use ax.pcolor or ax.pcolormesh instead, you could set the facecolor parameter to 'none' and the edgecolor parameter to 'k':

fig, ax = plt.subplots(1, 1)

x, y = np.meshgrid(lon_grid, lat_grid)
c = np.ones_like(x)

ax.pcolor(x, y, c, facecolor='none', edgecolor='k')

ax.set_xlim(lon_grid[0], lon_grid[-1])
ax.set_ylim(lat_grid[0], lat_grid[-1])

This is a very questionable hack, though.