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?

2 Answers 2

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.

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.
    – ricitron
    Dec 24, 2019 at 8:23
  • 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)
    – ricitron
    Dec 24, 2019 at 8:25
  • How can I manually create the colorbar with the proper alpha value?
    – ricitron
    Dec 24, 2019 at 8:50
  • @ricitron My apologies, you would be using a curved projection. pcolor and pcolormesh will both be insufficient there, you will have to imshow I believe Dec 24, 2019 at 10:03
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.

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 colorbars Dec 24, 2019 at 10:00
  • Is there an example for manually creating the desired colorbar? i.e., using a matplotlib color scale and setting alpha=0.5 for a colorbar
    – ricitron
    Dec 24, 2019 at 14:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.