我试图用卫星数据绘制一个简单的图。该数据包含lat,lon和sla(海平面异常)。我在本例中使用的数据位于here这个我尝试开始试验的简单脚本在这里: -
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from __future__ import print_function
import os
import platform
import numpy as np
from netCDF4 import Dataset
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap
width = 10
height = 8
nc_f = ('ctoh.sla.ref.RA2.medsea.0543.nc')
nc_fid = Dataset(nc_f, 'r')
lons = nc_fid.variables['lon'][:]
lats = nc_fid.variables['lat'][:]
sla = nc_fid.variables['sla'][:]
lon_0 = lons.mean()
lot_0 = lats.mean()
lon_max = nc_fid.variables['lon'].lon_max
lon_min = nc_fid.variables['lon'].lon_min
lat_min = nc_fid.variables['lat'].lat_min
lat_max = nc_fid.variables['lat'].lat_max
m = Basemap(projection='merc', lat_0=lot_0, lon_0=lon_0,
resolution = 'l', llcrnrlon=lon_min, llcrnrlat=lat_min,
urcrnrlon=lon_max, urcrnrlat=lat_max)
m.drawcoastlines()
m.drawcountries(linewidth=1.0)
# lons,lats= np.meshgrid(lons, lats)
x, y = m(lons, lats)
# plt.figure(figsize=(width, height), frameon=False)
# plt.contourf(x, y, sla)
print(y.shape)
print(x.shape)
print(sla.shape)
cs = m.contourf(x, y, sla)
plt.contourf(np.reshape(x, sla.shape), np.reshape(y, sla.shape), sla)
plt.show()
我面临的问题是lat和lon包含164的形状,而在底图上绘制的实际变量包含(164,83)的形状。这是我在终端中收到的完整错误消息: -
(164,)
(164,)
(164, 83)
Traceback (most recent call last):
File "satellite.py", line 39, in <module>
cs = m.contourf(x, y, sla)
File "/home/sundar/.anaconda2/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.py", line 521, in with_transform
return plotfunc(self,x,y,data,*args,**kwargs)
File "/home/sundar/.anaconda2/lib/python2.7/site-packages/mpl_toolkits/basemap/__init__.py", line 3644, in contourf
xx = x[x.shape[0]/2,:]
IndexError: too many indices for array
这是我第一次处理具有不同尺寸/形状的数据。如何绘制具有不同形状的这些数据?
答案 0 :(得分:2)
如果我正确理解Basemap.contourf
的文档,则需要对非结构化数据使用tri
关键字参数:
cs = m.contourf(lon, lat, sla, tri=True)
或可能
cs = m.contourf(x,y, sla, tri=True)
取决于您的数据。