简体   繁体   中英

Cartopy + Matplotlib (contourf) - Map Overriding data

I'm trying to do a Contour Plot having the Global Map in background. Having in mind that my data have LON and LAT values, I decided to use Cartopy with MatplotLib.

The problem is that I can plot my data and the map perfectly when separated, but when I try to integrate the data with the map the Cartopy map override my data plot.

This is my code:

ax = plt.axes(projection=cartopy.crs.PlateCarree())

v = np.linspace(0, 80, 25, endpoint=True)
cp = plt.contourf(matrixLon, matrixLat, matrixTec, v, transform=cartopy.crs.PlateCarree())
plt.colorbar(cp)

ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.set_extent([-85, -30, -60, 15])

plt.title('TEC Map')
plt.show()

Plots:

Plotting only Data

Plotting only the map

It is strange because I think that the logical is the data override the map (and maybe I have to try a transparent color scale) but not the other way around.

Can someone help me with this issue?

Here is the working code that you may try and learn.

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

# prep some data for contourf plot
# extents: upper-right of the map
x = np.linspace(-65, -30, 30)
y = np.linspace(-30, 15, 30)
matrixLon, matrixLat = np.meshgrid(x, y)
matrixTec = 10*np.sin(matrixLon**2 + matrixLat**2)/(matrixLon**2 + matrixLat**2)

ax = plt.axes(projection=cartopy.crs.PlateCarree())

# prep increasing values of v covering values of Z (matrixTec)
v = np.arange(-0.15, 0.15, 0.025)

# plot with appropriate parameters
# zorder: put the filled-contour on top
# alpha: set transparency to allow some visibility of graphics below
cp = plt.contourf(matrixLon, matrixLat, matrixTec, v, \
                  transform=cartopy.crs.PlateCarree(), \
                  zorder=2, \
                  alpha=0.65, \
                  cmap=plt.cm.copper)
plt.colorbar(cp)

ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.set_extent([-85, -30, -60, 15])

plt.title('TEC Map')
plt.show()

The essence is the use of zorder and alpha in plt.contourf() that can be set to show or hide some features on the map.

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM