How to apply a colour gradient for faces using mapshow command

1 回表示 (過去 30 日間)
Usman Shafique
Usman Shafique 2022 年 1 月 18 日
回答済み: Omega 2023 年 9 月 20 日
I am trying to draw various sqaures in a plot. each sqaure is supposed to display a unique value depending on its weight. Can any only help to explain how to assinga color gradient similar to jet for this case?
  2 件のコメント
Usman Shafique
Usman Shafique 2022 年 1 月 18 日
xbox = [0 0 8.46 8.46 0 ;0 0 8.46666667 8.466667 0 ;0 0 8.46666667 8.466667 0 ; 0 0 8.46666667 8.466667 0 ]
ybox = [ 0 5 5 0 0 ;5 10 10 5 5 ;10 15 15 10 10 ;15 20 20 15 15]
weight = [1,5,6,8
mapshow(xbox,ybox,'DisplayType','polygon','LineStyle','none')
Usman Shafique
Usman Shafique 2022 年 1 月 18 日
I want to create a colour gradient based on weight values and change the color of the polygon

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

回答 (1 件)

Omega
Omega 2023 年 9 月 20 日
Hi Usman,
I understand that you would like to create polygons on a plot, with each polygon being coloured according to its weight, and obtaining these colours from a predefined colour scheme.
To apply a colour gradient to the faces of polygons created with the mapshow command in MATLAB based on weight values, you can use the FaceColor property of the polygons. Here's how you can do it:
% Define your data
xbox = [0 0 8.46 8.46; 0 0 8.46666667 8.466667; 0 0 8.46666667 8.466667; 0 0 8.46666667 8.466667];
ybox = [0 5 5 0; 5 10 10 5; 10 15 15 10; 15 20 20 15];
weight = [1, 5, 6, 8];
% Define a colormap (you can change 'jet' to any other colormap like 'parula')
colormap('jet');
cmap = jet;
% Create a color scale based on the weight values
clim([min(weight), max(weight)]);
% Display polygons using mapshow and set the FaceColor based on weight
for i = 1:length(xbox)
color_value = weight(i); % Get the weight value for the current polygon
% Normalize color_value to the range [0, 1]
normalized_color = (color_value - min(weight)) / (max(weight) - min(weight));
% Map the normalized_color to a color from the colormap
color_index = round(normalized_color * (size(cmap, 1) - 1)) + 1;
polygon_color = cmap(color_index, :);
% Create a polygon object
polygon = mapshow(xbox(i, :), ybox(i, :), 'DisplayType', 'polygon', 'LineStyle', 'none');
% Set the FaceColor based on the mapped color
set(polygon, 'FaceColor', polygon_color);
hold on; % Keep the current axes for multiple polygons
end
% Set the color axis label
colorbar;
title('Polygon Color Gradient Based on Weight');
xlabel('X-axis');
ylabel('Y-axis');
To know more you can refer to the following documentation links:
I hope it helps to resolve your query!

カテゴリ

Help Center および File ExchangeElementary Polygons についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by