Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate white edges in Matplotlib/Basemap pcolor plot

I am plotting data on a map using this code:

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from mpl_toolkits.basemap import Basemap
from matplotlib.patches import Polygon
from scipy.io import netcdf

ncfile = netcdf.netcdf_file(myfile.nc,'r')
lon = ncfile.variables['longitude'][:]
lat = ncfile.variables['latitude'][:]
data = ncfile.variables['mydata'][:]
ncfile.close()

m = Basemap(projection='nplaea', boundinglat=40, lon_0=270)
m.drawcoastlines(linewidth=.6, zorder=2)
m.drawparallels(np.arange(-80.,81.,20.), zorder=1)
m.drawmeridians(np.arange(-180.,181.,20.), zorder=1)
cNorm = mpl.colors.Normalize(vmin=0, vmax=np.nanmax(data))
cmap = plt.get_cmap('jet')
lons, lats = np.meshgrid(lon, lat)
x, y = m(lons, lats)
datamap = m.pcolor(x, y, data, zorder=0)
datamap.set_norm(cNorm) 
plt.colorbar(datamap, cmap=cmap, norm=cNorm, shrink=0.5)
plt.savefig('figures/map_polar.png', dpi=150, bbox_inches='tight', pad_inches=0.4)

This results in this image: enter image description here

As you can see, there are white gaps between the grid cells. How can I get rid of them?

like image 644
HyperCube Avatar asked Nov 28 '12 13:11

HyperCube


1 Answers

I know this is a an old question but I thought i would add my solution to this problem. I found your question when I was having the exact same problem as yours, i.e. a white line in my plot and a grid going from -180 to 180. The solution for me was to use the Basemap function addcyclic

from mpl_toolkits.basemap import Basemap, shiftgrid, addcyclic
SSTcyclic, lonCMIP5cyclic = addcyclic(SST, lonCMIP5)

This solved my problem. Cheers, Trond

like image 77
Trond Kristiansen Avatar answered Oct 20 '22 21:10

Trond Kristiansen