如何将 Cartopy 等值线图更改为箱线图

发布于 2025-01-17 21:14:56 字数 2156 浏览 1 评论 0原文

我绘制了一个 CartoPy 等高线图,如下所示:

在此处输入图像描述

使用以下脚本:

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())

我现在意识到我更喜欢箱形图,每个网格框中都有一种纯色,我不再想要网格之间的插值点。

我发现我可以使用 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)

我看不到此错误意味着什么,也不知道如何修复它。有人做过类似的事情吗?

I have plotted a CartoPy contour plot which looks like this:

enter image description here

using the following script:

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())

I've now realized I'd prefer a box plot, with one solid colour in each grid box, I no longer want interpolation between grid points.

I found that I can use pcolormesh instead of contour to do this. However, when I change the last line of code:

plot = plt.pcolormesh(lon2d, lat2d, df, cmap = 'jet', transform=ccrs.PlateCarree())

I get the following error:

TypeError: Dimensions of C (15, 21) are incompatible with X (15) and/or Y (15)

I can't see what this error means to know how to fix it. Has anyone done anything similar?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放手` 2025-01-24 21:14:56

好的,既然您已经问过...这是一个快速示例,如何使用 eomaps 。 。


也可以通过m.new_layer_from_file.netcdf(...)

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()

“

OK, since you've asked... here's a quick example how you can do it with EOmaps...

Note that data and coordinates can be provided as 1D or 2D arrays (or mixtures of 1D and 2D as below) or as a pandas.DataFrames.
It's also possible to plot directly from a NetCDFs (or GeoTIFFs) via m.new_layer_from_file.NetCDF(...)

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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文