1

I have a matrix with 24 samples and 10,000 datapoints x each with different intensities y which I am able to plot in a line plot doing plot(x,y), however I have a third variable z (24x1) which is categorical. I am trying to plot this such that each sample in the same category is the same colour, but is yet to work.

An example of what z looks like is (A, B, C, A, C, B, ...).

So far, I have tried

plot(x, y, 'color', z)

but I get a warning:

 Error using plot
 Color value must be a 3 element vector

All guides I have found online give similar methods, none of which work.

I know that in R I would be able to just do something of the form

matplot(x, y, color = z)

but I have no experience with matlab, hence the confusion.

The expected outcome is a plot, where each sample of the same group in z is the same colour. Any help is greatly appreciated.

Edit:

Here is some example data. X can just be the column number

Sample    1.    2.    3.    4.    5.    Group
1         5     6     6     7     3     A
2.        4     4     6     5     2     B
3.        7     5     4     6     4     A
4.        5     6     3     4     3     C

So, sample 1 and 3 should be the same colour

Beavis
  • 476
  • 3
  • 13
  • Since you didn't give sample data it is difficult to give you a complete answer, but I would suggest you look at this question and answer: [Change color of 2D plot line depending on 3rd value](https://stackoverflow.com/questions/31685078/change-color-of-2d-plot-line-depending-on-3rd-value/31699594#31699594) – Hoki Aug 05 '19 at 10:38
  • @Hoki this solution does not work with my data, and I have added example data now – Beavis Aug 05 '19 at 12:13

3 Answers3

1

One way is to plot the data group for group, MATLAB will automatically assign a different color in that case:

samples = [ ...
    5     6     6     7     3
    4     4     6     5     2    
    7     5     4     6     4    
    5     6     3     4     3];    

groups = {...
    'A' 
    'B' 
    'A'
    'C' };

% generate x-values
x  = repmat(1:size(samples,2), size(samples,1), 1);


axes(figure);
hold on;
handles = {};

for group = unique(groups')
    idx = (strcmp(groups, group{1}));
    xplot = x(idx,:);
    splot = samples(idx,:);
    handles{end+1} = plot(xplot(:), splot(:),'o');
end

To access the Line object and change its properties, use for example:

handles{3}.MarkerFaceColor = 'r';
Patrick Happel
  • 1,336
  • 8
  • 18
1

You need to scan the category column (your z variable) and find the indices of the lines which belong to each category. Once you have that, you plot the lines by groups (belonging to the same category) and assign their reserved color.

One way of doing it:

%% Sample input data
Y = [   5     6     6     7     3
        4     4     6     5     2    
        7     5     4     6     4    
        5     6     3     4     3 ];    

z = {...
    'A' 
    'B' 
    'A'
    'C' };

x = 1:size(Y,2) ; % just defined to be able to use the notation "plot(x,Y)"
Y = Y.' ;         % Matlab is column major so transposed the matrix to have
                  % it the natural style (also can use "plot(x,Y)" this way)

%% Assign color for each category
% define any color you want for each category
categories_and_colors = { ...
    'A' , [1 0 0] ; % 'Red'     for category 'A'
    'B' , [0 1 0] ; % 'Green'   for category 'B'
    'C' , [0 0 1]   % 'Blue'    for category 'C'
    } ;

%% Find indices of vectors for each category
ncat = size(categories_and_colors,1) ;

plotcat = cell(ncat,1) ;
for k=1:ncat
    thisCategory    = categories_and_colors{k,1} ;
    idxThisCategory = strcmpi( thisCategory , z ) ;
    plotcat{k}      = idxThisCategory ;
end
% the variable [plotcat] is a cell array which contains a cell per
% category. Each cell contains the logical indices of the matrix lines
% belonging to this category

%% Plot
figure
hold on
for k=1:ncat
    plot( x , Y(:,plotcat{k}) , 'Color' , categories_and_colors{k,2} ) ;
end

Of course the code can be compacted once you understand the main flow. This will yield: enter image description here

Hoki
  • 11,637
  • 1
  • 24
  • 43
0

This code should work

[~,~,types] = unique(category);
colormap jet
cmap = colormap;
con_min = 0;
con_max = max(types);
ind_c = round((size(cmap,1)-1)*types/(con_max-con_min))+1;
set(gca,'ColorOrder',cmap(ind_c,:),'NextPlot','replacechildren');
plot (x, y);
Beavis
  • 476
  • 3
  • 13