首页 > 解决方案 > 带有纬度和经度位置的熊猫数据框的轮廓

问题描述

我有一个包含 450 个位置的结果数据框。我想用 contourf 绘图,但似乎对以下代码有问题:

使用的数据框:

Results

​</p>

   WS        Lat       Lon
0   -8.394130   38.05   -102.62
1   -1.424622   40.05   -104.37
2   -10.795424  35.55   -108.87
3   -4.925160   36.55   -101.37
4   -5.485772   32.05   -102.62
... ... ... ...
445 -5.010812   44.55   -90.12
446 -3.847437   40.55   -93.87
447 -15.534713  47.55   -90.12
448 -5.306695   45.55   -96.87
449 -9.107669   43.05   -90.62

450 rows × 3 columns

绘图代码:

plt.figure(figsize=(15,12))

central_lat = 37.5
central_lon = -96

ax = plt.axes(projection=ccrs.PlateCarree())

ax.coastlines()
ax.add_feature(cfeature.STATES, linewidth=1.5,edgecolor='black')

cp = ax.contourf(Results["Lon"], Results["Lat"], Results["WS"],cmap=plt.cm.get_cmap('bwr', 22),levels=[-5,-4.5,-4,-3.5,-3,-2.5,-2,-1.5,-1,-0.5,0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5],alpha=0.6,transform = ccrs.PlateCarree(),extend='both')
cb=plt.colorbar(cp,orientation="horizontal",pad=0.1)
cb.set_label(label='Difference in long-term mean wind speed (%)',size='large')#, weight='bold')

# Define the xticks for longitude
ax.set_xticks(np.arange(-126,-65,10), crs=ccrs.PlateCarree())
lon_formatter = cticker.LongitudeFormatter()
ax.xaxis.set_major_formatter(lon_formatter)

# Define the yticks for latitude
ax.set_yticks(np.arange(25,51,5), crs=ccrs.PlateCarree())
lat_formatter = cticker.LatitudeFormatter()
ax.yaxis.set_major_formatter(lat_formatter)

我得到的错误:

TypeError:输入 z 必须是二维数组。

在这种情况下如何制作 za 2D 数组?我在数据框中有 450 个位置对。关于如何让它发挥作用的任何想法?

标签: pythonpandascontourf

解决方案


推荐阅读