Sunday, March 13, 2011

Matlab Plot Example Change colors and symbols through a for loop

Also, see my gmail for attached function same subject line.


Matlab. control color (colors in a plot).

Some times you want to be able to set your own colors instead of letting the default be determined. A very nice reference is here:
http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html

A detailed Example:

Here is the symbols all setup:

%2. PLOT
%note:
%1. b = blue
%2. k = black
%For all see:
%http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html
colorcell = {'k', 'b','g', 'r', 'y', 'm', 'c', 'w'};
symbolcell ={'.','o', 'x',  '+', '-','*', ':', '-.'};
%plot hold on:
figure(1)

%plot the first value (and then loop over J)
plot(ValCell{1}, IDCell{1}, symbolcell{1}, 'color', colorcell{1})
xlim([0.5 4.5])
hold on;

for i=2:J
  %plot(ValCell{i}, IDCell{i}, 'o','color', colorcell{i})
  plot(ValCell{i}, IDCell{i}, symbolcell{i},'color', colorcell{i})

end

legend('True =1', 'True = 2', 'True = 3', 'True = 4')
hold off;

Very nice reference

http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html


function [IndexCell ValCell IDCell] = PlotclassMLR(sampleID, y, yhat, J)
%PlotclassMLR = Plot classify multinomial logistic regression data.
%This gives one way to graphically depict the classification accuracy.
%See the doc folder.
%INPUTS:
% *sampleID - n x 1. Gives the corresponding sample ID for the values in y and yhat.
%Note: when you produce the test set, you can keep the original SampleID
% *y - n x 1. The true response values
% *yhat - n x 1. The corresponding predicted response variables,
% *J - positive integer. Number of categories
%OUTPUTS:
% null - a graph is produced. SampleID vs. predicted label; true labels are show
%by different colors.

%1. Make the indices for each of the true labels.
IndexCell = cell(J,1);
ValCell = IndexCell;
IDCell = IndexCell;
for i=1:J

       %1. %Note IndexCell{j} gives the indices in y which correspond to true label = j
       IndexCell{i} = find(y == i);

       %2. ValCell = value Cell - keep now the yhat, but sorted by the true
       %value. Hence, ValCell{1} gives the predicted values for all of the respnose
       %which have true value = 1.
       ValCell{i} = yhat(IndexCell{i});

       %3. Similarly we need to store the SampleID to plot them
       %(in a simple example, just equal to the index, but in general,
       %sample ID can be different).
       IDCell{i} = sampleID(IndexCell{i});

end


%2. PLOT
%note:
%1. b = blue
%2. k = black
%For all see:
%http://web.cecs.pdx.edu/~gerry/MATLAB/plotting/symbolPlots.html
colorcell = {'k', 'b','g', 'r', 'y', 'm', 'c', 'w'};
symbolcell ={'.','o', 'x',  '+', '-','*', ':', '-.'};
%plot hold on:
figure(1)

%plot the first value (and then loop over J)
plot(ValCell{1}, IDCell{1}, symbolcell{1}, 'color', colorcell{1})
xlim([0.5 4.5])
hold on;

for i=2:J
       %plot(ValCell{i}, IDCell{i}, 'o','color', colorcell{i})
       plot(ValCell{i}, IDCell{i}, symbolcell{i},'color', colorcell{i})

end

legend('True =1', 'True = 2', 'True = 3', 'True = 4')
hold off;

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.