无法在 python 中使用 matplotlib 和 cartopy 绘制 contourf 子图
Cannot plot contourf subplots using matplotlib and cartopy in python
我试图在分析成子图后从 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.DataArray 'olr' (lat: 15, lon: 73)
anomaly_olr.olr
xarray.DataArray 'olr' (lat: 15, lon: 73)
它给出了这个没有 contourf 的图
在做了一些改动后,我可以很好地绘制它。我改变了几行如下,
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()
谢谢大家
我试图在分析成子图后从 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.DataArray 'olr' (lat: 15, lon: 73)
anomaly_olr.olr
xarray.DataArray 'olr' (lat: 15, lon: 73)
它给出了这个没有 contourf 的图
在做了一些改动后,我可以很好地绘制它。我改变了几行如下,
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()
谢谢大家