Wednesday, 23 October 2013

How to plot the shadow in the 'surf' plot in Matlab?

1. Follow these hints: Thanks to the 'shadowplot' function from Mathworks.
z = magic(100);
surf(z);
shading interp;
shadowplot x;
shadowplot y;

xlabel('xMagic','rot', 14,'fontweight','bold','fontsize',12); 
ylabel('yMagic','rot', -20,'fontweight','bold','fontsize',12);
zlabel('zMagic','fontweight','bold','fontsize',14);

Example of Shadow plot:


2. The shadowplot x and shadowplot y demand 'x' and 'y' to be monotonically increasing. Due to Matlab's floating point representation problem (clearly explained HERE), we will not get monotonically increasing number even when we use: dx = 0: 0.01:1; This can be checked by:

dx = 0:0.01:1;
figure;
plot(diff(dx));

This will become a problem to use 'shadowplot'. One method that could be used is:

a. Plot the 3-D space
surf(corrValues); % instead of surf(x,y, corrValues);
shading interp;
shadowplot y;
shadowplot x;

%Now the plot x and y indices will be integers starting from 1 to size(corrValues) respectively. Off course, there will be (0,0) point but corrValues are not pointed at this point.


b. Now to have the x- and y-axis labelling as desired, instead of integers, following method could be used:

% Get the string from the number array
for ii = 1:2:length(xx)
   xTickLabel1(ii).a = num2str(xx(ii), 4);
end

xlbl = {xTickLabel1.a};

for ii = 1:1:length(yy)
 yTickLabel1(ii).a = num2str(yy(ii), 4);
end

ylbl = {yTickLabel1.a};

% Set the grid-lines -- x and y indices where grid lines to be shown
set(gca, 'XTick',[1:1:length(xx)]);
set(gca, 'YTick',[1:1:length(yy)]);
% Label the grid-lines that are selected above.
set(gca,'XTickLabel',xlbl)
set(gca,'YTickLabel',ylbl)

No comments:

Post a Comment