import xarray as xr import numpy as np import matplotlib.pyplot as plt # Load the netCDF file ds = xr.open_dataset('../NC/FD_Char_1961-2020_1yrs_Daily_Reg_New.nc') # Extract the frq variable and longitude and time coordinates frq = ds['frq'] lon = ds['lon'] lat = ds['lat'] time = ds['month'] # Define the longitude range to average over lon1 = 101.125 lon2 = 110.875 # Calculate the longitude indices to average over lon_indices = np.where((lon >= lon1) & (lon <= lon2))[0] # Average over the longitude range frq_mean = frq.isel(longitude=lon_indices).mean(dim='longitude') # Create a time-latitude cross section plot of the averaged data fig, ax = plt.subplots() im = ax.pcolormesh(lat, time, frq_mean.T, cmap='viridis') ax.set_xlabel('Latitude') ax.set_ylabel('Time') ax.set_ylim([time[0], time[-1]]) cbar = fig.colorbar(im, ax=ax) cbar.set_label('frq') # Show the plot plt.show()