Question

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?

Was it helpful?

Solution 2

I had the same problem once. It's very likely the problem is in longitude. Make sure 0 and 360 both exist in the input. If not, manually add them, and make change to the mydata accordingly so that they have the same shape.

OTHER TIPS

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

It looks like to me like the original post is not actually asking about the white area between 0 and 360 degrees.

I think the OP is talking about the lines between each square of colour which would be consistent with this bug:

It seems that savig a pcolor plot to a pdf format always includes gridlines, which isn't true for other output formats like png

Here is what the developers say about the problem:

I see gridlines in the resulting image in gs, xpdf Preview.app but not in Adobe Reader. When I zoom in Preview, the lines jump around a bit, and are always the same width on the screen regardless of zoom level.

What gets drawn in this example is a lot of polygons, so that adjacent polygons share an edge with the exact same coordinates. The code fills the inside of each polygon, and apparently some rendering algorithms leave a minimal-width line between polygons.

So it's a problem with the PDF viewer, not with pcolor or any other aspect of matplotlib.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top