Eliminate white edges in Matplotlib/Basemap pcolor plot
Asked Answered
V

3

16

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?

Vampire answered 28/11, 2012 at 13:34 Comment(4)
can you put the data file up anyplace?Auction
Unfortunately, no, but it is regularly spaced gridded data..Vampire
For whatever it's worth, I can't reproduce your problem using randomly generated (or constant) data... What version of matplotlib are you using? Maybe its a bug that's been fixed? Also, pcolormesh will be faster in this case. Try using it instead of pcolor. I doubt (?) it will fix your problem, but it should be faster, regardless.Servility
Does pcolormesh work on a polar projection? (projection='nplaea') I get a really strange result using pcolormesh...(everything zero)Vampire
R
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.

Ridgley answered 2/12, 2012 at 0:2 Comment(1)
I am using a range of -180, 180. That should work, too? Indeed, 180 was missing, so I added it and copied the last column of the data to the data again. However, the white stripes still remain.Vampire
E
4

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.

Ellery answered 12/11, 2014 at 19:35 Comment(0)
A
3

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

Ardor answered 19/8, 2013 at 18:16 Comment(0)
R
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.

Ridgley answered 2/12, 2012 at 0:2 Comment(1)
I am using a range of -180, 180. That should work, too? Indeed, 180 was missing, so I added it and copied the last column of the data to the data again. However, the white stripes still remain.Vampire

© 2022 - 2024 — McMap. All rights reserved.