我想通过matplotlib在Python中画图

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

我想用 matplotlib 绘制图来显示经纬度随时间的温度变化。我得到这个:警告:x 坐标不是单调递增 - 等值线图可能不是您所期望的。如果看起来很奇怪,您可以调整地图投影区域以与您的数据一致,或者(如果您的数据位于全局纬度/经度网格上)使用 shiftgrid 函数调整数据以与地图投影区域一致(参见示例/contour_demo.py)

d=数据集('sst.mon.ltm.1991-2020.nc')

sst=d.variables['sst']

时间=d.variables['时间']

lon=d.variables['lon'][:]

lat=d.variables['lat'][:]

mean_sst=np.mean(sst,axis=(2),keepdims=False)

ax=无花果子图(121)

fig=plt.figure(figsize=(5,5)) 斧头=图.add_subplot(211) m = 底图(投影='merc', llcrnrlon= 0,urcrnrlon=180,llcrnrlat=-80,urcrnrlat=0) lat_grid,time_grid=np.meshgrid(lat,时间)

cx=m.contourf(lat_grid,time_grid,mean_sst,cmap='地震')

plt.show()

python matplotlib plot netcdf netcdf4
1个回答
0
投票

警告试图告诉您经度位于-180/180范围之外:

<class 'netCDF4._netCDF4.Variable'>
float32 lon(lon)
    units: degrees_east
    long_name: Longitude
    actual_range: [  0. 358.] # <<< here
    standard_name: longitude
    axis: X
    coordinate_defines: center
unlimited dimensions: 
current shape = (180,)
filling on, default _FillValue of 9.969209968386869e+36 used

因此,按照建议,您可以使用 

shiftgrid

 :
from mpl_toolkits.basemap import shiftgrid sst, lon = shiftgrid(180.0, sst, lon, start=False, cyclic=lon.max())

使用
this
数据集的

动画(参见完整代码):

enter image description here

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