Mapping issue with pcolor()
조회 수: 6 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Colormaps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!