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

57 visualizaciones (últimos 30 días)
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?

Respuesta aceptada

MathWorks Support Team
MathWorks Support Team el 24 de En. de 2024
Editada: MathWorks Support Team el 24 de En. de 2024
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 comentarios
Stephen23
Stephen23 el 11 de Oct. de 2018
"... 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 el 2 de Oct. de 2023
probably should use other variable names instead of 'length', which clashes with the default MATALB function

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Color and Styling en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by