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

조회 수: 59 (최근 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일
편집: 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