简体   繁体   English

Cartopy + Matplotlib(contourf)-地图覆盖数据

[英]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. 考虑到我的数据具有LON和LAT值,我决定将Cartopy与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. 问题是当分开时我可以完美地绘制数据和地图,但是当我尝试将数据与地图集成时,Cartopy地图会覆盖我的数据图表。

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. 本质是在plt.contourf()中使用zorderalpha ,可以将其设置为显示或隐藏地图上的某些要素。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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