Robert Samson
Robert Samson

Reputation: 1

I have a 2D plot in Matlab where I want to specify the RGB value for each point to vary to color

I am trying to plot a 2D line in Matlab with a color that varies based on an RGB code that I assign to each point. The code below works well for a given colormap ('col' values defining the color), but I am trying to keep tighter control over the color assignment, so that values always render the same color across multiple charts.

surface([x;x],[y;y],[z;z],[col;col],...
    'facecol','no',...
    'edgecol','interp',...
    'linew',2);

Upvotes: 0

Views: 537

Answers (1)

ASantosRibeiro
ASantosRibeiro

Reputation: 1257

The question is how do you decide which colour does each point have? defining the colormap is easy (shown below).

col=colormap(hot(128)) %% or other colour style like hsv or jet

if you have a variable (lets called it V) that for each point has a certain value and you want the colours to change based on that:

first define the values for the extremes in the colormap:

min_col=0; %%%can be the minimum of V
max_col=1; %%%can be the maximum of V

Then interpolate to your data

new_col=interp1(linspace(min_col,max_col,length(col)),col, V(:))

Upvotes: 1

Related Questions