Strange lines appear on pcolormesh in basemap when using a nonzero alpha value
Asked Answered
A

2

6

When plotting data using pcolormesh on a basemap projection (or a cartopy projection) I notice strange lines appear when I set the alpha value to less than 1.

Example code:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt

plt.clf()

dpp =1 # degrees per pixel
lons = np.arange(-180,180+dpp,dpp)
lats = -1*np.arange(-90,90+dpp,dpp)

m = Basemap(projection='robin',lon_0=0)
data = np.random.random((np.size(lats), np.size(lons)))
lons, lats = np.meshgrid(lons, lats)
x, y = m(lons, lats)

im = m.pcolormesh(x, y, x, latlon=False, cmap='RdBu')
#im = m.pcolormesh(lons, lats, data, latlon=True, cmap='RdBu')

m.colorbar(im)
plt.show()

The output shows strange lines appearing:

enter image description here

If I instead set alpha=1 the lines disappear and the behavior is as expected:

enter image description here

Any ideas on how to get pcolormesh to work with a nonzero alpha value?

Aplenty answered 23/12, 2019 at 18:50 Comment(0)
D
5

Use pcolor instead of pcolormesh, it is a bit slower but it does a better job with handling rasterized output. Be sure to set snap = True, this will align the grid to the pixels.

Example

import numpy as np
import matplotlib.pyplot as plt

lons, lats = np.meshgrid(np.arange(-180,180), np.arange(90,-90,-1))

im = plt.pcolor(lons, lats, lons, cmap='RdBu', alpha=0.5, snap=True)

cbar = plt.colorbar(im)
cbar.set_alpha(0.5)
plt.show()

This should work with mpl_toolkits.basemap as well.

enter image description here

The lines in the colorbar are caused by the open issue #1188, as far as I know there is not a work around known which does not involve manually creating the colorbar.

Draco answered 23/12, 2019 at 21:17 Comment(4)
I reproduced the figure of yours above (although there are still strange lines on the colorbar). However, when using basemap or cartopy I still get strange lines. So it still seems to be an issue when using a curved projection, like the robinson projection.Aplenty
Using the following lines in your script still had strange lines appear: import cartopy.crs as ccrs ____ ax = plt.axes(projection=ccrs.Robinson()) _____ im = ax.pcolor(lons, lats, lons, transform=ccrs.PlateCarree(),cmap='RdBu', alpha=0.5, snap=True)Aplenty
How can I manually create the colorbar with the proper alpha value?Aplenty
@Aplenty My apologies, you would be using a curved projection. pcolor and pcolormesh will both be insufficient there, you will have to imshow I believeDraco
A
0

Since it is a global map, I got it to work using imshow instead of pcolor or pcolormesh:

import numpy as np
import matplotlib.pyplot as plt
import cartopy.crs as ccrs

plt.clf()

lons, lats = np.meshgrid(np.arange(-180,180), np.arange(90,-90,-1))

im = ax.imshow(lons, transform=ccrs.PlateCarree(),cmap='RdBu', alpha=0.5, extent=[-180,180,-90,90])

cbar = plt.colorbar(im)
cbar.set_alpha(0.5)

plt.show()

enter image description here

There is still the issue with the colorbar however.

Aplenty answered 24/12, 2019 at 8:50 Comment(2)
As I mentioned in my answer the issue with the color bar is subject to an open issue on github without an existing workaround short of full manual handling of the color bar creation. Matplotlib simply doesn’t handle the alpha correctly for colorbarsDraco
Is there an example for manually creating the desired colorbar? i.e., using a matplotlib color scale and setting alpha=0.5 for a colorbarAplenty

© 2022 - 2024 — McMap. All rights reserved.