Thursday, September 24, 2009

Matlab Plotting within a loop with different colors

Matlab by default has 7 colors. Following code demonstrates how one can invoke plot command within a for loop so that each time it plots a given data set in a different color. Don't worry about the variables that are used in this piece of code. You should notice that we can pass randomized color information to the plot command inside a for loop.


hold on;
for k = 1:length(idx)
ridx = find(idx == k);
color = rand(1,3);
for j = 1:length(ridx);
plot(logdata(:,1,ridx(j)),logdata(:,2,ridx(j)), ...
'Color',color,'LineWidth',2);
end
end
hold off;

2 comments:

Swagat said...

This is how you can change the default color order in plot

>> set(gcf,'DefaultAxesColor',[1 0 0; 0 1 0; 0 0 1; 1 1 0]);
>> plot(data(:,1:2;7),data(:,2:2:8),'LineWidth',2)

Swagat said...

Another option would to use "varycolor" program written by Daniel Helmick. It provides a better interface to deal with multiple colors.

 
pre { margin: 5px 20px; border: 1px dashed #666; padding: 5px; background: #f8f8f8; white-space: pre-wrap; /* css-3 */ white-space: -moz-pre-wrap; /* Mozilla, since 1999 */ white-space: -pre-wrap; /* Opera 4-6 */ white-space: -o-pre-wrap; /* Opera 7 */ word-wrap: break-word; /* Internet Explorer 5.5+ */ }