How to rotate domain, but the lon and lat are on the contour (not on the axis)

8 views (last 30 days)
Dear all, as far we know Matlab will traditionally generate 4 white areas when using the following script on rotate domain:
figure;
p=pcolor(lon_rho,lat_rho,h2a);shading interp;
An example illustration is attached. Instead of generating a map like the illustration, I want the lon and lat to be directly placed on the generated contour, note the black numbers in the attachment (not on the matlab x and y axis). Is this possible?
and this is a perfect example of what I want to get:
  2 Comments
Dyuman Joshi
Dyuman Joshi on 28 Jun 2025
Moved: Dyuman Joshi on 29 Jun 2025
You can try something like this as showed in this thread.
Note that the aspect ratio of axes will get distorted as per the angle, so adjust them as per your requirement.
X = [1 2 3; 1 2 3; 1 2 3];
Y = X';
C = [3 4 5; 1 2 5; 5 5 5];
%Default orientation
pcolor(X,Y,C)
%Modified orientation
figure
axes
%rotate camera about viewing axis
camroll(30)
xlabel('X')
ylabel('Y')
%set(gca,'dataaspectratiomode','manual')
hold all
pcolor(X,Y,C)
As to why the ylabel moves to the right side, I do not know yet.
eko supriyadi
eko supriyadi on 28 Jun 2025
Moved: Dyuman Joshi on 29 Jun 2025
Thanks for the help Joshi, unfortunately that's not what I wanted. Because from the example of X, Y, and Z that you gave, it is basically not a rotation domain. Here are the variables I'm referring to (see attachment)
load rotate.mat
figure;
p=pcolor(lon_rho,lat_rho,z);shading interp;
and will result in a rotating domain like this:
I want the result to be Lon and Lat attached to the side of the contour (black box) instead of the conventional x and y-axis. How to do this in matlab?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jun 2025
Edited: Walter Roberson on 28 Jun 2025
You need to first calculate the tilt angle of the valid part of the data, then rotate by the negative of that angle so that the valid part would lie along the x axis. Then draw that, and camorbit() the resulting graph to tilt the plotted axes back to the original angle.
  2 Comments
eko supriyadi
eko supriyadi on 29 Jun 2025
Edited: eko supriyadi on 29 Jun 2025
Tks Walter, now I've been able to solve this problem, here's a script that might be useful for others:
% Rotational Transformation
tick_len = 0.055;
x = lon_rho;
y = lat_rho;
theta = median(angle(:)); % take the main angle of the domain
X_rot = cos(-theta)*(x - mean(x(:))) - sin(-theta)*(y - mean(y(:)));
Y_rot = sin(-theta)*(x - mean(x(:))) + cos(-theta)*(y - mean(y(:)));
% Data Plot on Rotated Grid
figure;
p = pcolor(X_rot, Y_rot, h); shading interp
hold on
axis off
% Add Manual Labels (lon/lat) at domain edge contour positions
% take 4 sides
[ny, nx] = size(lon_rho);
side_pts = {
[1:nx; ones(1,nx)], % top side
%[1:nx; ny*ones(1,nx)], % bottom side
%[ones(1,ny); 1:ny], % left side
%[nx*ones(1,ny); 1:ny] % right side
};
for k = 1:length(side_pts)
idx = sub2ind(size(lon_rho), side_pts{k}(2,:), side_pts{k}(1,:));
lon_labels = lon_rho(idx);
lat_labels = lat_rho(idx);
Xl = cos(-theta)*(lon_labels - mean(x(:))) - sin(-theta)*(lat_labels - mean(y(:)));
Yl = sin(-theta)*(lon_labels - mean(x(:))) + cos(-theta)*(lat_labels - mean(y(:)));
for i = 1:round(length(Xl)/8):length(Xl)
text(Xl(i), Yl(i)-0.1, sprintf('%.1f°E', lon_labels(i)), ...
'Color','k','FontSize',12,'FontWeight','bold', ...
'Rotation',0, 'HorizontalAlignment','center');
%make tick
lon_tick = lon_rho(1, i);
lat_tick = lat_rho(1, i);
x1 = cos(-theta)*(lon_tick - mean(x(:))) - sin(-theta)*(lat_tick - mean(y(:)));
y1 = sin(-theta)*(lon_tick - mean(x(:))) + cos(-theta)*(lat_tick - mean(y(:)));
plot([x1 x1], [y1 y1 - tick_len], 'k', 'LineWidth', 1);
%make grid
lon_line = lon_rho(:,i);
lat_line = lat_rho(:,i);
xg = cos(-theta)*(lon_line - mean(x(:))) - sin(-theta)*(lat_line - mean(y(:)));
yg = sin(-theta)*(lon_line - mean(x(:))) + cos(-theta)*(lat_line - mean(y(:)));
plot(xg, yg, 'k--', 'LineWidth', 0.5);
end
end
view(-16,25) % I favour view over camorbit
Torsten
Torsten on 29 Jun 2025
theta = median(angle(:)); % take the main angle of the domain
throws an error because there is no argument to the angle function.
p = pcolor(X_rot, Y_rot, h); shading interp
h is undefined.

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!