Mapping issue with pcolor()

6 次查看(过去 30 天)
Qian
Qian 2024-3-17
评论: Voss 2024-3-17
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

采纳的回答

Voss
Voss 2024-3-17
编辑:Voss 2024-3-17
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 个评论
Qian
Qian 2024-3-17
移动:Voss 2024-3-17
It works great! Thank you so much.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by