Assign a colormap to an array to plot with the nsidepoly function.

11 views (last 30 days)
Hi, this code plots a hexagonal mesh using the nsidedpoly function, where each hexagon is labeled with the corresponding r-value. The problem is that each hexagon is plotted independently and I can't apply any colormap only the red color (FaceColor.) How can I assign the values of a colormap (for example jet()) to the r-values depending on their value?
Thanks in advance.
x = load('COORDXY');
coord = [x(230:235, :)
x(256:262, :)
x(282:289, :)
x([308:310,312:316], :)
x(334:343, :)
x([360:364,366,368:370], :)
x(387:396, :)
x([414:416,418:422], :)
x(441:448, :)
x(468:474, :)
x(495:500, :)];
for i = 1:length(coord)
poly(i) = nsidedpoly(6, 'Center', coord(i, :), 'SideLength', 5.6617);
end
pg = plot(poly);
axis off
axis equal
a = 0.5;
b = 1.5;
r = (b-a).*rand(87, 1) + a;
cmap = jet(15);
colormap(jet(10));
for i = 1:length(pg)
pg(i).FaceColor = 'r';
end
colormap(jet(10));
cmap = jet(87);
str = num2str(r, 2);
t2 = text(coord(:, 1), coord(:, 2), str, ...
'HorizontalAlignment', 'center','VerticalAlignment','middle', 'FontSize', 10, 'FontName', 'Times');

Accepted Answer

Simon Chan
Simon Chan on 5 Mar 2022
You may try the following:
clear; clc;
coord = [1 1;4 4;7 7;10 10];
a = 0.5;
b = 1.5;
r = (b-a).*rand(4, 1) + a;
nLevel = 100; % Number of level in the colormap
cmap = colormap(jet(nLevel)); % Select your colormap
polyFaceColor = cmap(ceil((r-a)*nLevel),:); % Calculate which level of the colormap for each polygon
for i = 1:length(coord)
pgon(i) = nsidedpoly(6, 'Center', coord(i, :), 'SideLength', 2);
end
pg = plot(pgon);
axis off
axis equal
for i = 1:length(coord)
pg(i).FaceColor = polyFaceColor(i,:); % Assign the colormap to the polygon
end
str = num2str(r, 2);
t2 = text(coord(:, 1), coord(:, 2), str, ...
'HorizontalAlignment', 'center','VerticalAlignment','middle', 'FontSize', 10, 'FontName', 'Times');

More Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!