绘制地图:海洋表面水流的旋转箭头

时间:2013-07-08 14:08:50

标签: python numpy matplotlib matplotlib-basemap

以下是netCDF文件显示的变量: 1

我已编写此代码以使用netCDF文件绘制地中海表面电流:

import netCDF4
from netCDF4 import Dataset
import datetime as dt
import numpy as np
import numpy.ma as ma
from datetime import date, datetime, timedelta
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

nc=Dataset('datasets/essai.nc')

y = nc.variables['g0_lat_1']
y = np.array(y)

x = nc.variables['g0_lon_2']
x = np.array(x)

u = nc.variables['UOGRD_GDS0_DBSL']
u = np.array(u)
u = u[0, :, :]
u = np.deg2rad(u)

v = nc.variables['VOGRD_GDS0_DBSL']
v = np.array(v)
v = v[0, :, :]
v = np.deg2rad(v)

y, x = np.meshgrid(y,x)

plt.quiver(x [::10], y[::10], u[::10], v[::10], pivot='tail')
plt.show()

这给了我这个:

当我放大时,我们可以看到有箭头和点: enter image description here 首先,我不知道这些意思是什么,我无法想象海岸线。然后,我的所有箭头都朝着同一个方向前进!所以我在论坛上找到了这段代码:

# Calculate positions of vectors on map projection 
y, x = np.meshgrid(lat,lon)

# Calculate the orientation of the vectors
x1, y1 = m(lon+u, lat+v)
u_map, v_map = x1-x, y1-y

# Rescale the magnitudes of the vectors...
mag_scale = np.hypot(u_map, v_map) / np.hypot(u, v)
u_map /= mag_scale
v_map /= mag_scale

m.quiver(x, y, u_map, v_map)
plt.show()

但它只给我一个箭头。你有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试修改以下代码以适合您的数据,basemap网站上也有一些有用的示例。但是,如果要发布这些图表,最好使用ncl,它有大量工具可用于绘制气象/海洋数据。

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap

x = np.linspace(-10, 40, 10)
y = np.linspace(25, 45, 10)
lons, lats = np.meshgrid(x, y)

u = np.random.random(np.shape(lons))
v = np.random.random(np.shape(lats))


m = Basemap(llcrnrlon=-10.,llcrnrlat=25.,urcrnrlon=40.,urcrnrlat=45.,
            lon_0=10., lat_0=35,
            projection='lcc', resolution ='l')

m.drawcoastlines()

X,Y = m(lons,lats)
m.quiver(X,Y,u,v)

plt.show()