Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change color of 2D plot line depending on 3rd value

I have a data set 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

Where the first column corresponds to time (one second intervals between data points) and will be on the x axis, the second column corresponds with distance and will be on the y axis. The third column is a number (one through five) that is a qualification of the movement.

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

I've seen a lot of posts about making a sliding scale of colors depending on an intensity value, but I just want 5 colors: (red, orange, yellow, green, and blue) respectively.

I tried doing something like this:

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

But with no luck.

Any ideas of how to approach this? Without looping if possible.

like image 909
JSZ Avatar asked Jul 28 '15 19:07

JSZ


People also ask

How do you specify colors in a plot?

To specify colors with either approach, call the desired plotting function with an output argument so you can access the individual plot objects. Then set properties on the plot object you want to change. For example, create a scatter plot with 100-point filled markers.

How do I change the color scheme of a plot in Matlab?

You can change the color scheme by specifying a colormap. Colormaps are three-column arrays containing RGB triplets in which each row defines a distinct color. For example, here is a surface plot with the default color scheme. f = figure; surf(peaks);

How do I color a line in Matlab?

Create a line plot and use the LineSpec option to specify a dashed green line with square markers. Use Name,Value pairs to specify the line width, marker size, and marker colors. Set the marker edge color to blue and set the marker face color using an RGB color value.

How do you plot a red line in Matlab?

Create a plot with a red dashed line and circular markers by specifying the linespec argument as '--or' . For this combination, '--' corresponds to a dashed line, 'o' corresponds to circular markers, and 'r' corresponds to red. You do not need to specify all three aspects of the line.


2 Answers

You can also do it with a trick which works with Matlab version anterior to 2014b (as far back as 2009a at least).
However, is will never be as simple as you expected (unless you write a wrapper for one of the solution here you can forget about plot(x,y,{'r','o','y','g','b'})).

The trick is to use a surface instead of a line object. Surfaces benefit from their CData properties and a lot of useful features to exploit color maps and texture.

Matlab surf does not handle 1D data, it needs a matrix as input so we are going to give it by just duplicating each coordinate set (for example xx=[x,x]).
Don't worry though, the surface will stay as thin as a 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=[x x];           %// create a 2D matrix based on "X" column
yy=[y y];           %// same for Y
zz=zeros(size(xx)); %// everything in the Z=0 plane
cc =[c c] ;         %// 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 X-Y 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 the color associated to the y value:
cmapline2

like image 165
Hoki Avatar answered Sep 21 '22 02:09

Hoki


Do you have Matlab R2014b or higher?

Then you could use some undocumented features introduced 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

like image 32
Robert Seifert Avatar answered Sep 23 '22 02:09

Robert Seifert