无法在python中使用matplotlib和cartopy绘制轮廓图子图

无法在python中使用matplotlib和cartopy绘制轮廓图子图,python,matplotlib,subplot,cartopy,Python,Matplotlib,Subplot,Cartopy,我试图在分析后将NetCDF文件中的2个图形绘制成子图,我可以单独绘制,但不能作为子图绘制。没有显示错误,但图形显示不正确 import xarray as xr import matplotlib.pyplot as plt import cartopy.crs as ccrs import numpy as np import cartopy.mpl.ticker as cticker #%% OLR Import fname ='/home/SIMS/P1/*.nc'

我试图在分析后将NetCDF文件中的2个图形绘制成子图,我可以单独绘制,但不能作为子图绘制。没有显示错误,但图形显示不正确

import xarray as xr
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import numpy as np
import cartopy.mpl.ticker as cticker

#%% OLR Import
fname ='/home/SIMS/P1/*.nc'                                              
ds = xr.open_mfdataset(fname)

olr = ds.olr
mea = ds.mean('time')

# OLR daily data import
cname ='/home/SIMS/olrr/OLR.nc'
dc = xr.open_dataset(cname)
colr = dc.olr

# calculating JJAS climatology 
def is_jjas(month):
    return (month >= 6) & (month <= 9)

seasonal_data_olr = colr.sel(time=is_jjas(colr['time.month']))
climatology = seasonal_data_olr.mean('time')
# Anomaly
anomaly_olr=mea-climatology



fig = plt.figure()
ax = plt.axes(projection=ccrs.Mercator(central_longitude=80.0, min_latitude=-10.0, max_latitude=20.0, globe=None, latitude_true_scale=0.0))
plt.subplot(2,1,1)
climatology.plot.contourf(ax=ax,transform=ccrs.PlateCarree(),vmin=0, vmax=245)
plt.subplot(2,1,2)
anomaly_olr.olr.plot.contourf(ax=ax, transform=ccrs.PlateCarree(),cmap="jet", vmin=-30, vmax=30)
plt.show()
将xarray作为xr导入
将matplotlib.pyplot作为plt导入
将cartopy.crs作为CCR导入
将numpy作为np导入
将cartopy.mpl.ticker作为cticker导入
#%%OLR导入
fname='/home/SIMS/P1/*.nc'
ds=xr.open_mfdataset(fname)
olr=ds.olr
mea=ds.平均值(‘时间’)
#OLR每日数据导入
cname='/home/SIMS/olrr/OLR.nc'
dc=xr.open_数据集(cname)
colr=dc.olr
#计算JJAS气候学
def是_jjas(月):

return(month>=6)和(month在做了一些更改后,我可以很好地绘制它

plt.figure(figsize=(8, 10))
ax1 = plt.subplot(2,1,1, projection=ccrs.Mercator(central_longitude=80.0, min_latitude=-10.0, max_latitude=20.0, globe=None, latitude_true_scale=0.0))
anomaly_olr.olr.plot.contourf(ax=ax1, transform=ccrs.PlateCarree(),cmap="jet", vmin=-30, vmax=30)

ax2 = plt.subplot(2,1,2, projection=ccrs.Mercator(central_longitude=80.0, min_latitude=-10.0, max_latitude=20.0, globe=None, latitude_true_scale=0.0))
climatology.plot.contourf(ax=ax2,transform=ccrs.PlateCarree(),vmin=0, vmax=245)
plt.show()

无论如何,谢谢

您能提供一个完整的示例吗?我添加了更多的代码。我可以单独绘制每个绘图,但不能作为子绘图。@Keldorn您可能没有包含NetCDF文件。我没有该文件,因为它不是一个可复制的示例。@Keldorn任何NetCDF文件都可以,它是一个简单的子绘图。我不认为重新绘制有那么困难不管怎样,我已经解决了这个问题并给出了我的解决方案