【发布时间】:2019-09-23 14:32:49
【问题描述】:
我有一个包含三列的数据框:Latitude、Longitude 和 Variable。其中大约有 100K 行。我需要绘制这些数据的颜色网格,显示奇怪的线条和区域。
我在各处都看到过使用网格网格转换一维纬度数组的帖子,但在这种情况下这不起作用,因为它们本身就是特定的坐标。我试图按升序对它们进行排序 lon 和/或 lat 和/或两者。然后我尝试移动坐标,将它们重新塑造成它们总长度的因子,但徒劳无功。
数据:
Longitude = [-10, -40, 34, 12, 67, ...] # 138627 elements
Latitude = [ 23, -89, 67, -25, 92, ...] # same
Variable = [ 1, 2, 3, 4, 5, ...] # same
代码:
import cartopy, glob, warnings, os, matplotlib.pyplot as plt
import numpy as np, cartopy.crs as ccrs
from netCDF4 import Dataset as netcdf_dataset
from cartopy import config
from mpl_toolkits.basemap import Basemap
warnings.simplefilter('ignore')
# CARTOPY
ax = plt.axes(projection=ccrs.PlateCarree())
plt.pcolormesh(Longitude, Latitude, Variable)
ax.coastlines()
plt.show()
# BASEMAP
m = Basemap(projection='cyl', llcrnrlat=-90, llcrnrlon=-180, rcrnrlat=90, urcrnrlon=180)
#TRY 1
new_coor = sorted([(i,j) for i,j in zip(Longitude, Latitude)], key=lambda x: x[0])
shape = (3,46209) #len(Latitude) = 138627
Longitude = np.asarray([i[0] for i in new_coor]).reshape(shape)
Latitude = np.asarray([i[1] for i in new_coor]).reshape(shape)
xi, yi = m(Longitude, Latitude)
cs = m.pcolor(xi, yi, np.squeeze(Variable)) #tried *pcolormesh* also
# tried *m.shifting()* and *lat_lon = True* also
m.drawcoastlines()
m.drawcountries()
cbar = m.colorbar(cs, location='bottom', pad="10%")
plt.show()
【问题讨论】:
-
您需要以某种方式将数据放入二维网格中才能使用
pcolormesh,或使用tripcolor对其进行三角测量。要对数据进行网格化,请查看 docs.scipy.org/doc/scipy/reference/generated/… 或其中的链接。
标签: python matplotlib matplotlib-basemap cartopy