How to generate and use different colours on a plot and label as per the colours?

50 views (last 30 days)
Hello all
I want to know how i can generate different colours and use those in my figure and then place on the figure that what colours represents what. My figure will look like the attached image, its only a 3 case example but my final image consits of large number of cases which are variable. In example image I want to add futher the labelling scheme performing like legend,showing colour=abc(pharase) etc.
I hope to get an answer soon
Regards

Accepted Answer

dpb
dpb on 26 May 2014
plot(rand(10,4)) % plot 4 lines in default color cycle
legend('a', 'b', 'c', 'd') % label them -- colors show automagically
text(2,0.05,'Stuff in green','color','g') % add some text in a color
You can create custom colors or select the named colors from a list--there are many examples in the doc. See the doc on the various functions above and the links on annotation and enhancement in the Graphics section.
  5 Comments
dpb
dpb on 26 May 2014
You can't get the linestyle drawn as '*,-' unless you were to make a custom legend using annotation and/or text. Guess that's doable if you were to create the legend, then remove the line markers and add a text at the location they take up.
I can get you the combination marker of line with the sympbol as follows if that'll do in a pinch...
[h_leg,h_obj]=legend([p1(1),p2(1),p3(1)],'p0','p1','p2');
set(h_obj(4:2:end),{'linestyle'},{':'})
The first three object handles are the text; that'll go up w/ every additional text line you add so the lines are the following six. The 3 visible are the even ones, hence the 4:2:end. If you swap the px(1) for px(2), then it'll be the odd ones and you'd set the marker instead of the linestyle.

Sign in to comment.

More Answers (2)

Mischa Kim
Mischa Kim on 26 May 2014
Edited: Mischa Kim on 26 May 2014
Sameer, something like
x = 0:0.1:1;
y = sin(x);
z = cos(x);
plot(x,y,'r-d',x,z,'b-o',x,y.*z,'g-*')
legend('x vs y','x vs z','x vs y*z')
xlabel('x')
ylabel('y, z, y*z')
  3 Comments
Mischa Kim
Mischa Kim on 26 May 2014
Check out
data1 = 10*ones(10,2) + randn(10,2); % test data 1: two sets
t1 = (0:1:9);
figure();
p1 = plot(t1,data1,'r-d');
hold on
data2 = 20*ones(100,2) + 2*randn(100,2); % test data 2: two sets
t2 = (0:0.1:9.9);
p2 = plot(t2,data2,'b-o');
data3 = 30*ones(100,2) + 3*randn(100,2); % test data 3: two sets
t3 = (0:0.1:10);
p3 = plot(t2,data3,'g-*');
legend([p1(1),p2(1),p3(1)],'From data1','From data2','From data3');
Sameer
Sameer on 26 May 2014
Hello
Thank you...its almost working for me but want to add something. Please see the attached image and the code for it is
>> PD0=[23,25,28];
>> P0=[27,29,32];
>> Re0=[2000,2600,3000];
>> PD1=[37,39,42];
>> PD1=[33,35,38];
>> P1=[37,39,42];
>> Re1=[2300,2800,3400];
>> PD2=[37,42,48];
>> P2=[39,44,52];
>> Re2=[2600,3100,3800];
>> p1=plot(Re0,PD0,'r*',Re0,P0,'r');
>> hold on
>> p2=plot(Re1,PD1,'b*',Re1,P1,'b');
>> p3=plot(Re2,PD2,'g*',Re2,P2,'g');
>> legend([p1(1),p2(1),p3(1)],'p0','p1','p2');
now as the legend is showing only * and the name,what I want is *,- and the name. I know only * is displayed because we used p1(1), and displays - when p1(2) is used but i am not able to find a way to display both. Hope you can guide.
Awaiting your reply.
Regards

Sign in to comment.


dpb
dpb on 27 May 2014
I just had another thought -- don't know if it'll work or not and I cleaned out the trial stuff so don't have it to play w/ at the moment, but:
If you were to use both sets of handles but one with blank text, maybe you could get both symbols one below the other with just the one text line...something like
  • a-
  • b-
As said, not sure if can get that to happen or not, just a passing fancy of a thought...
ADDENDUM:
Well, I did try it and, by golly, it does work. Not quite what you were looking for ideally, but not too awful bad I'd judge...see what you think w/ this--
[h_leg,h_obj]=legend([p1(:).' p2(:).' p3(:).'],'p0',' ','p1',' ','p2',' ');

Categories

Find more on Line Plots in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!