Clear Filters
Clear Filters

How to apply a colour gradient for faces using mapshow command

6 views (last 30 days)
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 Comments
Usman Shafique
Usman Shafique on 18 Jan 2022
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 on 18 Jan 2022
I want to create a colour gradient based on weight values and change the color of the polygon

Sign in to comment.

Answers (1)

Divit
Divit on 20 Sep 2023
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!

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!