【发布时间】:2017-01-19 16:44:09
【问题描述】:
我正在尝试使用卫星数据绘制一个简单的图。该数据包含 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
这是我第一次处理具有不同尺寸/形状的数据。这些具有不同形状的数据是如何绘制的?
【问题讨论】:
-
我有点困惑,你到底想画什么?如果我理解正确,您有 164 个纬度/经度对和每对 83 个数据点?这 83 点应该如何解释?
-
实际上,查看此示例可能对docs 有所帮助。可能值得检查示例数据源(这似乎需要安装
osgeo库)。我还会考虑将问题名称从matplotlib更改为basemap,因为我发现它与matplotlib完全不同。 -
你没有。文档字符串中明确说明:
*X* and *Y* must both be 2-D with the same shape as *Z*, or they must both be 1-D such that ``len(X)`` is the number of columns in *Z* and ``len(Y)`` is the number of rows in *Z*.
标签: python matplotlib plot