Assigning positions to multiple figures along-z-axis.
42 次查看(过去 30 天)
显示 更早的评论
hi,
I have a code that generates multiple contour filled plots using a for loop. i want to position the each result at different positions along the z-axis as when i am running the code, it overwrites the previous image and not able to change the position along-z-axis. here is the code. I have tried hold function but how can i hold the plots one on top of the other. As i want multiple 2D contour plots in one matlab figure seperated by distance along the z-axis.
Or if there is a way to access already saved contourfilled plots and plotting them on top of other.
Code:
figure(1);
for i=1:1:7
hold on
slice= MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised(:,:,i);
x=X_reconstr(:,:,i);
y=Y_reconstr(:,:,i);
limit=[0.6 1];
contourf(x,y,slice,'LineStyle' , 'none');
view(3)
caxis(limit)
colorbar
grid on
axis normal
end
using this code i am getting this result.
but instead of this i want results like this and i want to position them along -zaxis. Yours help will be greatly appreciated, i would be greatfull if you suggest me solution to this. thanks.
0 个评论
采纳的回答
Walter Roberson
2021-9-29
contourf() always represents the data at z = 0.
You need to do something like this:
fig = figure(1);
ax = gca(fig);
hold(ax, 'on')
z_per_slice = 5;
z_base = 1;
for i=1:1:7
slice= MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised(:,:,i);
x=X_reconstr(:,:,i);
y=Y_reconstr(:,:,i);
limit=[0.6 1];
tform = hgtransform(ax);
contourf(tform, x,y,slice,'LineStyle' , 'none');
tform.Matrix = makehgtform('translate', [0, 0, z_base + z_per_slice*(i-1)]);
end
hold(ax, 'off')
view(3)
xlim(ax, 'auto')
ylim(ax, 'auto')
zlim(ax, 'auto')
caxis(limit)
colorbar(ax)
grid(ax, 'on')
axis(ax, 'normal')
6 个评论
Walter Roberson
2021-9-30
Note:
zlim auto did not work to figure out the range of transformed coordinates
legend show showed only one empty legend entry, so the handles had to be accumulated.
M = 20; N = 30; S = 7;
MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised = sort(.5 + .6 * rand(M,N,S),2);
[X_reconstr, Y_reconstr, ~] = ndgrid(linspace(0,1,M), linspace(0,pi/2,N), 1:S);
fig = figure(1);
ax = gca(fig);
z_per_slice = 5;
z_base = 1;
limit=[0.6 1];
for i=1:1:S
slice= MAG_uwb_scatt_incoh_reconstr_diff_OLD_normalised(:,:,i);
x=X_reconstr(:,:,i);
y=Y_reconstr(:,:,i);
tform = hgtransform(ax);
hold(ax, 'on')
[~, h(i)] = contourf(x,y,slice,'LineStyle' , 'none', 'parent', tform, 'displayname', string(i));
tform.Matrix = makehgtform('translate', [0, 0, z_base + z_per_slice*(i-1)]);
end
hold(ax, 'off')
view(3)
xlim(ax, 'auto')
ylim(ax, 'auto')
caxis(limit)
colorbar(ax)
grid(ax, 'on')
axis(ax, 'normal')
legend(h)
zlim(ax, [z_base z_per_slice*(S-1)+z_base])
更多回答(1 个)
Image Analyst
2021-9-29
You can probably adapt this code:
% Takes an RGB image and stacks the separate color channels at different Z levels.
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 13;
filename = 'peppers.png';
rgbImage = imread(filename);
% Extract the individual red, green, and blue color channels.
redChannel = im2double(rgbImage(:, :, 1));
greenChannel = im2double(rgbImage(:, :, 2));
blueChannel = im2double(rgbImage(:, :, 3));
H(1) = slice(repmat(redChannel,[1 1 2]),[],[], 1); %slice() requires at least 2x2x2
set(H(1),'EdgeColor','none') %required so image isn't just an edge
hold on
H(2) = slice(repmat(greenChannel,[1 1 2]),[],[], 2); %slice() requires at least 2x2x2
set(H(2),'EdgeColor','none') %required so image isn't just an edge
H(3) = slice(repmat(blueChannel,[1 1 3]),[],[], 3); %slice() requires at least 2x2x2
set(H(3),'EdgeColor','none') %required so image isn't just an edge
hold off
colormap(gray(256))
axis ij
caption = sprintf('R, G, and B Color Channels of %s', filename);
title(caption, 'FontSize', fontSize);
% Put up legend that says what slice is what color channel.
legend('B', 'G', 'R')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Red 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!