Assign a colormap to an array to plot with the nsidepoly function.
5 次查看(过去 30 天)
显示 更早的评论
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/915039/image.png)
0 个评论
采纳的回答
Simon Chan
2022-3-5
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');
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Blue 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!