[英]How do I change a Cartopy contour plot into box plot
我繪制了一個 CartoPy 輪廓 plot,它看起來像這樣:
使用以下腳本:
precip_full1 = xr.open_dataset('era_yr1979.nc')
precip_full2 = xr.open_dataset('era_yr1980.nc')
precip_full3 = xr.open_dataset('era_yr1981.nc')
precip_full4 = xr.open_dataset('era_yr1982.nc')
precip_full5 = xr.open_dataset('era_yr1983.nc')
precip_full6 = xr.open_dataset('era_yr1984.nc')
precip_full = xr.concat([precip_full1,precip_full2,precip_full3,precip_full4,precip_full5,precip_full6],dim = 'time')
output = []
for x in np.arange(6.5,10.25,0.25):
for y in np.arange(-15,-9.75,0.25):
precip = precip_full.where((precip_full.latitude==x)&(precip_full.longitude==y),drop=True)
roll = precip.rolling(time=6,center=False).sum()
annual = roll.groupby('time.year').max()
tab = annual.to_dataframe().rename(columns={'tp':6})
output = pd.concat(output,1)
mean = output.mean()
data_mean = pd.DataFrame(mean, columns=['mean'])
df = data_mean.to_numpy()
new = [df[i:i+21] for i in range(0,len(df),21)]
new = np.reshape(new, [-1, 21])
df = pd.DataFrame(data=new, dtype=object)
lon2d, lat2d = np.meshgrid(lon, lat)
plt.figure(figsize=(6,5))
ax = plt.axes(projection=ccrs.PlateCarree())
ax.set_extent([-15,-10,6.5,10])
ax.coastlines()
ax.add_feature(cfeature.LAND)
ax.add_feature(cfeature.LAKES)
ax.add_feature(cfeature.RIVERS)
ax.add_feature(cfeature.BORDERS)
gl = ax.gridlines(draw_labels=True, xlocs=np.arange(-180,180,0.25), ylocs=np.arange(-90,90,0.25),linewidth=0.4)
gl.top_labels = False
gl.right_labels = False
plot = plt.contourf(lon2d, lat2d, df, cmap = 'jet', transform=ccrs.PlateCarree())
我現在意識到我更喜歡一個框 plot,每個網格框中有一個純色,我不再需要網格點之間的插值。
我發現我可以使用 pcolormesh 而不是輪廓來做到這一點。 但是,當我更改最后一行代碼時:
plot = plt.pcolormesh(lon2d, lat2d, df, cmap = 'jet', transform=ccrs.PlateCarree())
我收到以下錯誤:
TypeError: Dimensions of C (15, 21) are incompatible with X (15) and/or Y (15)
我看不出這個錯誤意味着什么,不知道如何修復它。 有沒有人做過類似的事情?
好吧,既然你已經問過了……這里有一個簡單的例子,你可以如何使用EOmaps來做到這一點……
請注意,數據和坐標可以作為 1D 或 2D arrays(或下面的 1D 和 2D 的混合)或pandas.DataFrames
。 也可以通過m.new_layer_from_file.NetCDF(...)
直接從 NetCDF(或 GeoTIFF)獲取 plot
from eomaps import Maps
import numpy as np
# create some data in a regular lon/lat grid (=epsg 4326)
x, dx = np.linspace(-45, 45, 55, retstep=True)
y, dy = np.linspace(-20, 30, 25, retstep=True)
vals = np.random.randint(0,100, (x.size, y.size))
# plot the data as lon/lat rectangles on a map displayed in Orthographic projection.
m = Maps(Maps.CRS.Orthographic())
m.add_feature.preset.coastline()
m.set_data(vals, x, y, crs=4326)
m.set_shape.rectangles(radius=(dx/2, dy/2), radius_crs=4326)
m.plot_map()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.