Change the color of a 2D plot line depending on the third value

I have a dataset that looks like this:

140400 70.7850 1 140401 70.7923 2 140402 70.7993 3 140403 70.8067 4 140404 70.8139 5 140405 70.8212 3 

If the first column corresponds to time (one interval between data points) and will be on the x axis, the second column corresponds to the distance and will be on the y axis. The third column is a number (from one to five), which is the qualification of the movement.

I want to make a plot that changes the color of the line between two points depending on what happened with the number of the previous data point. For example, I want the line to be red between the first and second data points, because the qualification value was 1.

I saw a lot of messages about creating a sliding color scale depending on the intensity value, but I just want 5 colors: (red, orange, yellow, green and blue) respectively.

I tried to do something like this:

 plot(x,y,{'r','o','y','g','b'}) 

But no luck.

Any ideas on how to approach this? No looping if possible.

+5
source share
3 answers

You can also do this with a trick that works with the Matlab version before 2014b (back in 2009a at least).
However, it will never be as easy as you expected (if you do not write a wrapper for one of the solutions here, you can forget about plot(x,y,{'r','o','y','g','b'}) ).

The trick is to use surface instead of the line object. Surfaces benefit from their CData properties and many useful features for using color maps and textures.

Matlab surf does not process 1D data, it needs a matrix as input, so we are going to give it by simply duplicating each set of coordinates (for example, xx=[x,x] ).
Do not worry, although the surface will remain as thin as the line, so the end result is not ugly.

 %% // your data M=[140400 70.7850 1 140401 70.7923 2 140402 70.7993 3 140403 70.8067 4 140404 70.8139 5 140405 70.8212 3]; x = M(:,1) ; %// extract "X" column y = M(:,2) ; %// same for "Y" c = M(:,3) ; %// extract color index for the custom colormap %% // define your custom colormap custom_colormap = [ 1 0 0 ; ... %// red 1 .5 0 ; ... %// orange 1 1 0 ; ... %// yellow 0 1 0 ; ... %// green 0 0 1 ; ... %// blue ] ; %% // Prepare matrix data xx=[xx]; %// create a 2D matrix based on "X" column yy=[yy]; %// same for Y zz=zeros(size(xx)); %// everything in the Z=0 plane cc =[cc] ; %// matrix for "CData" %// draw the surface (actually a line) hs=surf(xx,yy,zz,cc,'EdgeColor','interp','FaceColor','none','Marker','o') ; colormap(custom_colormap) ; %// assign the colormap shading flat %// so each line segment has a plain color view(2) %// view(0,90) %// set view in XY plane colorbar 

will get you:
cmapline


As an example of a more general case:

 x=linspace(0,2*pi); y=sin(x) ; xx=[x;x]; yy=[y;y]; zz=zeros(size(xx)); hs=surf(xx,yy,zz,yy,'EdgeColor','interp') %// color binded to "y" values colormap('hsv') view(2) %// view(0,90) 

will give you a sine wave with a color corresponding to the y value:
cmapline2

+7
source

Do you have Matlab R2014b or higher?

Then you can use some undocumented functions provided by Yair Altman :

 n = 100; x = linspace(-10,10,n); y = x.^2; p = plot(x,y,'r', 'LineWidth',5); %// modified jet-colormap cd = [uint8(jet(n)*255) uint8(ones(n,1))].' %' drawnow set(p.Edge, 'ColorBinding','interpolated', 'ColorData',cd) 

enter image description here

+4
source

My desired effect was achieved below (simplified):

  indices(1).index = find( data( 1 : end - 1, 3) == 1); indices(1).color = [1 0 0]; indices(2).index = find( data( 1 : end - 1, 3) == 2 | ... data( 1 : end - 1, 3) == 3); indices(2).color = [1 1 0]; indices(3).index = find( data( 1 : end - 1, 3) == 4 | ... data( 1 : end - 1, 3) == 5); indices(3).color = [0 1 0]; indices(4).index = find( data( 1 : end - 1, 3) == 10); indices(4).color = [0 0 0]; indices(5).index = find( data( 1 : end - 1, 3) == 15); indices(5).color = [0 0 1]; % Loop through the locations of the values and plot their data points % together (This will save time vs. plotting each line segment % individually.) for iii = 1 : size(indices,2) % Store locations of the value we are looking to plot curindex = indices(iii).index; % Get color that corresponds to that value color = indices(iii).color; % Create X and Y that will go into plot, This will make the line % segment from P1 to P2 have the color that corresponds with P1 x = [data(curindex, 1), data(curindex + 1, 1)]'; y = [data(curindex, 2), data(curindex + 1, 2)]'; % Plot the line segments hold on plot(x,y,'Color',color,'LineWidth',lineWidth1) end 
0
source

All Articles