I am making a program that plots a variable amount of inputs from a database. Suffice to say that I have a variable number of arrays that I need to plot on the same figure. I can get the variables to plot fine with a loop.
for i=1:n
plot(array1(:,i),array2(:,i),'o')
end
This plots i arrays where array1 holds the x values and array 2 holds the y values.
I was wondering if:
a: There is a way to change the colors, in other words, make each dataset show up with different colors.
b: make a legend that coorsponds to the colors. I would like to see which data goes with which plot. I can pull the names from an array...I mainly need some way to color the different plots differently.

댓글 수: 4

midhun
midhun 2014년 4월 28일
편집: midhun 2014년 4월 28일
A simple rand function will generate this. add 'color',rand(1,3) ... Thats it...
eg:
for i=1:n
plot(array1,array2,'color',rand(1,3));
end
Ryan Rizzo
Ryan Rizzo 2018년 4월 16일
@midhun, grazzi. This worked. Thanks
Karam Haidar
Karam Haidar 2020년 5월 23일
does this literally only work with the numbers 1 and 3? I had a similar problem and it spit out an error with every other combination I tried!
Stephen23
Stephen23 2020년 5월 23일
"does this literally only work with the numbers 1 and 3?"
Yes, because RGB triples are defined as a vector of three values.
"I had a similar problem and it spit out an error with every other combination I tried!"
The documentation clearly explains what the valid inputs are for the 'color' option:
Reading the documentation is a much more efficient way to write code than just trying random changes.

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2011년 5월 31일

17 개 추천

You can make a cell array of your chosen colors, then:
C = {'k','b','r','g','y',[.5 .6 .7],[.8 .2 .6]} % Cell array of colros.
x = 0:.01:1;
hold on
for ii=1:7
plot(x,x.^ii,'color',C{ii},'marker','o')
end
legend({'x.^1';'x.^2';'x.^3';'x.^4';'x.^5';'x.^6';'x.^7'})
With the legend command, the first string in the list corresponds to the first line plotted, etc. Another approach would be to use one of the MATLAB colormaps:
CM = jet(n); % See the help for COLORMAP to see other choices.
then:
for ii=1:n
plot(array1(:,ii),array2(:,ii),'color',CM(ii,:),'marker','o')
end

댓글 수: 3

Minkush Kansal
Minkush Kansal 2017년 12월 17일
Thanks a lot! It works nicely :)
Could you please explain how does the definition of cell array of colors ( 1st line of code) work?
i.e. C = {'k','b','r','g','y',[.5 .6 .7],[.8 .2 .6]} --how does this work exactly?
Leonard Zensen
Leonard Zensen 2021년 12월 7일
☺️
Snigdha
Snigdha 2024년 10월 25일
it does not work, man!!!!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Oleg Komarov
Oleg Komarov 2011년 5월 31일

4 개 추천

you can simply, and it will automatically change the colors for each series:
plot(array1,array2,'o')
Check out the legend command

카테고리

태그

질문:

2011년 5월 31일

댓글:

2024년 10월 25일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by