How can I programatically create a custom color gradient for a plot or markers?

51 次查看(过去 30 天)
I would like to create a plot with a color gradient that ranges from red to almost white.  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. 
What is the simplest way to create this color vector programmatically?

采纳的回答

MathWorks Support Team
编辑:MathWorks Support Team 2024-1-24
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 example (i.e. [255, 192, 203]), and then scale the values down to the range of [0,1]. This can then be used to generate a linearly spaced vector "colors_p" which provides a smoother gradient from red to pink.  See the code below for an example:
% 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
colorMapLength = 5;
red = [1, 0, 0];
pink = [255, 192, 203]/255;
colors_p = [linspace(red(1),pink(1),colorMapLength)', linspace(red(2),pink(2),colorMapLength)', linspace(red(3),pink(3),colorMapLength)'];
% 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, as shown in the below example:
surf(peaks)
colorMapLength = 5;
red = [1, 0, 0];
pink = [255, 192, 203]/255;
colors_p = [linspace(red(1),pink(1),colorMapLength)', linspace(red(2),pink(2),colorMapLength)', linspace(red(3),pink(3),colorMapLength)'];
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.
  2 个评论
Stephen23
Stephen23 2018-10-11
"... now would like to store this custom colormap in a matrix that I can use in subsequent plots using the colormap()"
map = colormap(adjFigH);
colormap(newFigH,map)
Where adjFigH is the handle to the figure that you have adjusted the colormap in, and newFigH is another figure/axes that you want to use that colormap in.
Walter Roberson
Walter Roberson 2023-10-2
probably should use other variable names instead of 'length', which clashes with the default MATALB function

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Color and Styling 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by