Cartopy + Matplotlib(contourf)-地图覆盖数据 [英] Cartopy + Matplotlib (contourf) - Map Overriding data

查看:31
本文介绍了Cartopy + Matplotlib(contourf)-地图覆盖数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试绘制以全局地图为背景的等高线图.考虑到我的数据具有 LON 和 LAT 值,我决定将 Cartopy 与 MatplotLib 结合使用.

问题是当分开时我可以完美地绘制数据和地图,但是当我尝试将数据与地图集成时,Cartopy地图会覆盖我的数据图表.

这是我的代码:

  ax = plt.axes(projection = cartopy.crs.PlateCarree())v = np.linspace(0,80,25,端点=真)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地图')plt.show()

图:

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.

这篇关于Cartopy + Matplotlib(contourf)-地图覆盖数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆