如何修复重叠的Metpy / Cartopy图像?

问题描述 投票:0回答:1

当我运行此代码时

import Scientific.IO.NetCDF as S
import cartopy.crs as ccrs
import cartopy.feature as cfeature
import matplotlib.pyplot as plt
import xarray as xr
import metpy
import numpy as N

from metpy.plots import ContourPlot, ImagePlot, MapPanel, PanelContainer
# Any import of metpy will activate the accessors
import metpy.calc as mpcalc
#from metpy.testing import get_test_data
from metpy.units import units
# Open the netCDF file as a xarray Datase


#
datadir='C:/Users/stratus/AppData/Local/lxss/home/stratus/PROJECT/NEWPROJECT/FEB012017/nam_218_20170131_1200_000.nc'
data = xr.open_dataset(datadir,decode_cf=True)
# To parse the full dataset, we can call parse_cf without an argument, and assign the returned
# Dataset.
data = data.metpy.parse_cf()
tempatt=data['TMP_P0_L100_GLC0'].attrs

# If we instead want just a single variable, we can pass that variable name to parse_cf and
# it will return just that data variable as a DataArray.
data_var = data.metpy.parse_cf('TMP_P0_L100_GLC0')

# To rename variables, supply a dictionary between old and new names to the rename method
data.rename({
    'TMP_P0_L100_GLC0': 'temperature',
}, inplace=True)

data['temperature'].metpy.convert_units('degC')

# Get multiple coordinates (for example, in just the x and y direction)
x, y = data['temperature'].metpy.coordinates('x', 'y')

# If we want to get just a single coordinate from the coordinates method, we have to use
# tuple unpacking because the coordinates method returns a generator
vertical, = data['temperature'].metpy.coordinates('vertical')
data_crs = data['temperature'].metpy.cartopy_crs


# Or, we can just get a coordinate from the property
#time = data['temperature'].metpy.time

# To verify, we can inspect all their names
#print([coord.name for coord in (x, y, vertical, time)])

#
#heights = data['height'].metpy.loc[{'time': time[0], 'vertical': 850. * units.hPa}]
#lat, lon = xr.broadcast(y, x)
#f = mpcalc.coriolis_parameter(lat)
#dx, dy = mpcalc.grid_deltas_from_dataarray(heights)
#u_geo, v_geo = mpcalc.geostrophic_wind(heights, f, dx, dy)
#print(u_geo)
#print(v_geo)
fig=plt.figure(1)
# A very simple example example of a plot of 500 hPa heights

data_crs = data['temperature'].metpy.cartopy_crs

ax = plt.axes(projection=ccrs.LambertConformal())
data['temperature'].metpy.loc[{'vertical': 850. * units.hPa}].plot(ax=ax, transform=data_crs)

ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.OCEAN)
ax.add_feature(cfeature.COASTLINE)
plt.show()
#ax.set_extent([-120,-80,20,50])
plt.title("850 mb Temperature")
#plt.suptitle("Metpy Test")
plt.show()

我不得不根据一些答案来编辑代码,但是现在我得到的大部分是空白地图。 850 T Map fail我主要是想使美国的温度达到850 mb,所以我可以把它展示给一个朋友来练习一个我正在帮助他的项目。数据括号的填充有所帮助,这就是为什么我对其进行编辑的原因。

matplotlib cartopy metpy
1个回答
0
投票
如评论中所指出,没有可复制的示例很难回答。但是,以下方法可以解决您的问题:

data_crs = data['temperature'].metpy.cartopy_crs ax = plt.axes(projection=ccrs.LambertConformal()) data['temperature'].metpy.loc[{'vertical': 1000. * units.hPa}].plot(ax=ax, transform=data_crs) ax.add_feature(cfeature.LAND) ax.add_feature(cfeature.OCEAN) ax.add_feature(cfeature.COASTLINE) plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.