putting a plot as different slices on an axis

I want to use matlab to plot in different time-steps an agent who is moving to different places in this structure:
pgon = polyshape([-0.5 -0.6882; 0.5 -0.6882; 0.5 -1.6882;-0.5 -1.6882]);
pgon2 = polyshape([0.5 -0.6882; 0.806 0.2629; 1.7571 -0.0431;1.4511 -0.9972]);
pgon3 = polyshape([0.806 0.2629; 0 0.8507; 0.5878 1.6567;1.3938 1.0689]);
pgon4 = polyshape([0 0.8507; -0.809 0.2629; -1.3968 1.1136;-0.5878 1.6597]);
pgon5 = polyshape([ -0.809 0.2629; -0.5 -0.6882; -1.4511 -0.9972;-1.7601 -0.0461]);
plot([pgon1 pgon pgon2 pgon3 pgon4 pgon5]);
axis equal;
Now I have an array of positions for these 6 cells and I want to put this sturcture (if it is possible) as different slices in an axis so for N=20 then I can color code where my agent stands in this structure at each n. It would be like a 3D structure that allows me to see the positions in each n=1,..,20 in this plotted structure. Is it possible to do it?

 採用された回答

Cris LaPierre
Cris LaPierre 2021 年 1 月 20 日

1 投票

One thought is to create your geometry using patches, which allow you to specify a z coordinate.
pgon1 = [-0.5 -0.6882; 0.5 -0.6882; 0.5 -1.6882;-0.5 -1.6882];
pgon2 = [0.5 -0.6882; 0.806 0.2629; 1.7571 -0.0431;1.4511 -0.9972];
pgon3 = [0.806 0.2629; 0 0.8507; 0.5878 1.6567;1.3938 1.0689];
pgon4 = [0 0.8507; -0.809 0.2629; -1.3968 1.1136;-0.5878 1.6597];
pgon5 = [ -0.809 0.2629; -0.5 -0.6882; -1.4511 -0.9972;-1.7601 -0.0461];
patch(pgon1(:,1),pgon1(:,2),ones(length(pgon1),1),'r')
patch(pgon2(:,1),pgon2(:,2),ones(length(pgon2),1)*2,'y')
patch(pgon3(:,1),pgon3(:,2),ones(length(pgon3),1)*3,'g')
patch(pgon4(:,1),pgon4(:,2),ones(length(pgon4),1)*4,'b')
patch(pgon5(:,1),pgon5(:,2),ones(length(pgon5),1)*5,'m')
axis equal tight
view(3)
grid on

8 件のコメント

Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 1 月 20 日
well, sound great but I can't see how I make a couple plots from this star structure in different slices??
Cris LaPierre
Cris LaPierre 2021 年 1 月 20 日
編集済み: Cris LaPierre 2021 年 1 月 20 日
If it helps, I can't envision what it is you are trying to do at all. However, what "slice" it goes on is specified by the Z input, ones(length(pgon5),1)*5.
Perhaps you want a star on each value of N?
ax=axes;
hold on
for n=1:5
star(ax,n)
end
hold off
axis equal tight
grid on
view([-12.93 60.64])
zlabel('N')
function star(ax,n)
pgon1 = [-0.5 -0.6882; 0.5 -0.6882; 0.5 -1.6882;-0.5 -1.6882];
pgon2 = [0.5 -0.6882; 0.806 0.2629; 1.7571 -0.0431;1.4511 -0.9972];
pgon3 = [0.806 0.2629; 0 0.8507; 0.5878 1.6567;1.3938 1.0689];
pgon4 = [0 0.8507; -0.809 0.2629; -1.3968 1.1136;-0.5878 1.6597];
pgon5 = [ -0.809 0.2629; -0.5 -0.6882; -1.4511 -0.9972;-1.7601 -0.0461];
patch(ax,pgon1(:,1),pgon1(:,2),ones(length(pgon1),1)*n,'r')
patch(ax,pgon2(:,1),pgon2(:,2),ones(length(pgon2),1)*n,'y')
patch(ax,pgon3(:,1),pgon3(:,2),ones(length(pgon3),1)*n,'g')
patch(ax,pgon4(:,1),pgon4(:,2),ones(length(pgon4),1)*n,'b')
patch(ax,pgon5(:,1),pgon5(:,2),ones(length(pgon5),1)*n,'m')
end
Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 1 月 20 日
編集済み: Zahra Sheikhbahaee 2021 年 1 月 20 日
Thanks it looks nice. One other question is that How can I color code each of these squares based on probability between zero and one by the degree of being light or dark blue or green color? Any suggestion? Thanks.
Cris LaPierre
Cris LaPierre 2021 年 1 月 20 日
編集済み: Cris LaPierre 2021 年 1 月 20 日
The final input is the color. See the Polygon Colors section of the documentation for all the available options.
You'll probably want to specify that in the main script, and pass it into your function. See the function documentation page if you need help with that.
Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 1 月 20 日
can I do something like this:
Cdata=d;
cmap = colormap('gray');
% make it into a index image.
cmin = min(0);
cmax = max(1);
m = length(cmap);
index = fix((Cdata-cmin)/(cmax-cmin)*m)+1; %A
% Then to RGB
RGB = ind2rgb(index,cmap)
where d=0.2 or d=0.5 and then the prblem is that if I give RGB value to patch as color it gets error. any better way to generate my color in a range between 0 and 1?
Cris LaPierre
Cris LaPierre 2021 年 1 月 20 日
That appears to be making things way more complicated than they need to be.
You can specify an RGB triplate as the color, but there are other ways as well. Valid inputs include
  • scalar | vector | matrix | RGB triplet | 'r' | 'g' | 'b' | ...
If you use RGB, note that the values have to be between 0 and 1, not 0 and 255.
If you have a single value representing probability, then it might be easier to just use a vector input. Look in the table under the "One color per face" row.
  • n-by-1 vector of colormap colors, where n is the number of faces. The CDataMapping property determines how the values map into the colormap.
Zahra Sheikhbahaee
Zahra Sheikhbahaee 2021 年 1 月 20 日
final question and I will appreciate a lot: How can I write a text in each cell of slices ?
Cris LaPierre
Cris LaPierre 2021 年 1 月 20 日
I would probably attempt to do this using the text function.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by