Conni
Conni

Reputation: 25

Coincident Bivariate Map with Squares

I want to display temperature change and uncertainty in the same map with Cartopy in Python. I would like to have the temperature change indicated by colors (cmap = 'coolwarm') and the uncertainty by the size of the squares... The colorbar schould look like in this picture. So far, I could only find how to overlay two colormaps to create a 2D colormap, but not how to use color vs. size. Is there any package that might help or is it possible to do this just with maptlotlib and cartopy?

Upvotes: 0

Views: 126

Answers (1)

Rutger Kassies
Rutger Kassies

Reputation: 64443

You could consider using Matplotlibs hatches to basically overplot some white gridded pattern on top of your data. I'm not sure how to make the hatches align nicely between the different "densities".

Generating some sample data:

def sample_data(nlats=180, nlons=360):
    # https://scitools.org.uk/cartopy/docs/latest/gallery/scalar_data/waves.html#sphx-glr-gallery-scalar-data-waves-py

    lats, lons = np.mgrid[-np.pi/2:np.pi/2:np.pi/nlats, 0:2*np.pi:2*np.pi/nlons]    
    wave = 0.75 * (np.sin(2 * lats) ** 8) * np.cos(4 * lons)
    mean = 0.5 * np.cos(2 * lats) * ((np.sin(2 * lats)) ** 2 + 2)
    
    return np.rad2deg(lons), np.rad2deg(lats), wave + mean

lons, lats, data = sample_data()

_, _, uncert = sample_data(360,720)
uncert = uncert.T[:180, :360]

Plotting:

fig, ax = plt.subplots(figsize=(10,5), dpi=86, subplot_kw=dict(projection=ccrs.EckertIII()))

ax.set_global()
ax.coastlines('110m', alpha=0.5)

img = ax.contourf(lons, lats, data, cmap=plt.cm.coolwarm, alpha=0.8, transform=ccrs.PlateCarree())
pat = ax.contourf(lons, lats, uncert, hatches=['+'*1, '+'*2, '+'*3, '+'*4], alpha=1.0, transform=ccrs.PlateCarree())

for p in pat.collections:
    p.set_facecolor('none')
    p.set_edgecolor('w')
    p.set_linewidth(0.0)

enter image description here

Upvotes: 1

Related Questions