MATLAB: How to programatically create a custom color gradient for a plot or markers

colorcolormapcolormapeditorcustomeditormanualmapMATLABprogramatically

 I need to create a plot with a color gradient that ranges from red to almost white. What is the simplest way to create a color vector? For example, I need 5 intermediate colors in a color vector where the darkest color is [1 0 0] and the lightest one is [1 1 1] in such a way that colors = [ [1 0 0] [? ? ?] [? ? ?] [? ? ?] [? ? ?] [? ? ?] [? ? ?] 1 1 1] ].  This means creating a color gradient manually in such a way that it ranges from red to light pink. 

Best Answer

To create colors manually and assign them to the marker values, start with the RGB values of last desired color in the shade, say light pink for this example (i.e. [255, 192, 203]) and then scale the values down to [0,1]. These are then used to generate a linearly spaced vector "colors_p" which provides a smoother gradient from red to pink.
% Display map of the world using default Plate Carree projection
geoshow('landareas.shp', 'FaceColor', [0 0 0]);
% create a default color map ranging from red to light pink
length = 5;
red = [1, 0, 0];
pink = [255, 192, 203]/255;
colors_p = [linspace(red(1),pink(1),length)', linspace(red(2),pink(2),length)', linspace(red(3),pink(3),length)'];
% plot random markers on the map and assign them the colors created
S=10; % marker size
geoshow(randi([-90,90]),randi([-180,180]), 'DisplayType', 'point','marker','^','MarkerEdgeColor','k','MarkerFaceColor',colors_p(1,:),'markersize',S); hold on;
geoshow(randi([-90,90]),randi([-180,180]), 'DisplayType', 'point','marker','^','MarkerEdgeColor','k','MarkerFaceColor',colors_p(2,:),'markersize',S); hold on;
geoshow(randi([-90,90]),randi([-180,180]), 'DisplayType', 'point','marker','^','MarkerEdgeColor','k','MarkerFaceColor',colors_p(3,:),'markersize',S); hold on;
geoshow(randi([-90,90]),randi([-180,180]), 'DisplayType', 'point','marker','^','MarkerEdgeColor','k','MarkerFaceColor',colors_p(4,:),'markersize',S); hold on;
geoshow(randi([-90,90]),randi([-180,180]), 'DisplayType', 'point','marker','^','MarkerEdgeColor','k','MarkerFaceColor',colors_p(5,:),'markersize',S); hold on;
legend('a', 'b', 'c', 'd', 'e', 'Location', 'northwestoutside')
legend('boxoff')
These custom colors can also be applied to "colormap" of any surface plot. See the following example,
surf(peaks)
length = 5;
red = [1, 0, 0];
pink = [255, 192, 203]/255;
colors_p = [linspace(red(1),pink(1),length)', linspace(red(2),pink(2),length)', linspace(red(3),pink(3),length)'];
colormap(colors_p)
Note: Using the "colormapeditor" GUI is a neat way of generating these color vector, since the results can be visually adjusted and modified on the go. For an opened figure, this can be done by changing the number of 'Node Pointers' and adjusting them to desired color values in 'colormapeditor' window.