Mapping issue with pcolor()

5 visualizzazioni (ultimi 30 giorni)
Qian
Qian il 17 Mar 2024
Commentato: Voss il 17 Mar 2024
When I use pcolor() to show the raster data, there seems something wrong with the data, as shown in the figure below. But using pcolorm(), the data can be correctly mapped. Does anyone know why? The following code is provided by Austin M. Weberhttps://www.mathworks.com/matlabcentral/answers/2092826-changing-the-longitude-of-netcdf-from-0-360-to-180-180?s_tid=mlc_ans_email_view#comment_3101196.
% Repeating the first part
load cpc.mat
lat = double(lat);
lon = double(lon);
mask = lon > 180;
lon(mask) = lon(mask) - 360;
lat = flip(lat);
tmax = flipud(tmax');
% Repeating making a new netcdf file
file_name = 'new.nc';
ncid = netcdf.create(file_name, 'NETCDF4');
lat_len = length(lat);
lon_len = length(lon);
lat_dim = netcdf.defDim(ncid, 'lat', lat_len);
lon_dim = netcdf.defDim(ncid, 'lon', lon_len);
lat_var = netcdf.defVar(ncid, 'lat', 'double', lat_dim);
lon_var = netcdf.defVar(ncid, 'lon', 'double', lon_dim);
tmax_var = netcdf.defVar(ncid, 'tmax', 'double', [lat_dim, lon_dim]);
netcdf.endDef(ncid);
netcdf.putVar(ncid, lat_var, lat);
netcdf.putVar(ncid, lon_var, lon);
netcdf.putVar(ncid, tmax_var, tmax);
netcdf.close(ncid);
% Visualize
mync = ncread('new.nc','tmax');
lat = ncread('new.nc','lat');
lon = ncread('new.nc','lon');
figure(1)
subplot(2,1,1)
pcolor(lon, lat, mync)
title('Cartesian Axes with pcolor()')
colorbar
shading interp
axis equal tight
grid on
subplot(2,1,2)
axesm('eqdcylin')
pcolorm(lat, lon, mync)
title('Map Axes with pcolorm()')
colorbar
shading interp
tightmap
gridm
mlabel('south') % Longitude tick labels
plabel('on') % Latitude tick labels

Risposta accettata

Voss
Voss il 17 Mar 2024
Modificato: Voss il 17 Mar 2024
The discontinuity in lon where it changes from +180 to -180 causes the problem with pcolor, which you see as the blue streak in the upper part of the plot. The pcolor documentation says that, in order "to create a rectangular grid of vertices", if input X is a vector it must be "A vector containing values that are increasing or decreasing". With that discontinuity, the values of lon are not all-increasing or all-decreasing.
A simple way fix the pcolor plot is to plot based on a sorted copy lon and a copy of mync where its columns are rearranged in the same order. See lon_plot and mync_plot below.
% Repeating the first part
load cpc.mat
lat = double(lat);
lon = double(lon);
mask = lon > 180;
lon(mask) = lon(mask) - 360;
lat = flip(lat);
tmax = flipud(tmax');
% Repeating making a new netcdf file
file_name = 'new.nc';
ncid = netcdf.create(file_name, 'NETCDF4');
lat_len = length(lat);
lon_len = length(lon);
lat_dim = netcdf.defDim(ncid, 'lat', lat_len);
lon_dim = netcdf.defDim(ncid, 'lon', lon_len);
lat_var = netcdf.defVar(ncid, 'lat', 'double', lat_dim);
lon_var = netcdf.defVar(ncid, 'lon', 'double', lon_dim);
tmax_var = netcdf.defVar(ncid, 'tmax', 'double', [lat_dim, lon_dim]);
netcdf.endDef(ncid);
netcdf.putVar(ncid, lat_var, lat);
netcdf.putVar(ncid, lon_var, lon);
netcdf.putVar(ncid, tmax_var, tmax);
netcdf.close(ncid);
% Visualize
mync = ncread('new.nc','tmax');
lat = ncread('new.nc','lat');
lon = ncread('new.nc','lon');
figure(1)
subplot(2,1,1)
[lon_plot,idx] = sort(lon);
mync_plot = mync(:,idx);
pcolor(lon_plot, lat, mync_plot)
title('Cartesian Axes with pcolor()')
colorbar
shading interp
axis equal tight
grid on
subplot(2,1,2)
axesm('eqdcylin')
pcolorm(lat, lon, mync)
title('Map Axes with pcolorm()')
colorbar
shading interp
tightmap
gridm
mlabel('south') % Longitude tick labels
plabel('on') % Latitude tick labels
  2 Commenti
Qian
Qian il 17 Mar 2024
Spostato: Voss il 17 Mar 2024
It works great! Thank you so much.
Voss
Voss il 17 Mar 2024
You're welcome!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Colormaps in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by