Coloring curves wrt to a third variable and creating legend for the same

Hello, I need to plot 31 curves and their color needs to be according to a third variable that varies from 1 to 5. I tried writing the code as follows:
colorspec1 = colormap(jet(5));
for i = 1:31
for j = 1:5
if MA(i,2) == j
semilogx(All_112(:,1), All_112(:,i+1),'-', 'Linewidth', 2, 'Color', colorspec1(j,:));
hold all
end
legendInfo{j} = ['Wind Speed = ' num2str(j)];
end
end
legend(legendInfo);
I am getting curves with varying colors, but something is wrong with the code as when I change the loop positions and define j before i, the color of some curves also changes, which should not happen. I am unable to fix this issue and would need your help. Another issue is that I need a legend with 5 different colored lines in the colormap to define the third variable, but unable to get that with this version of the code. Could you please give me some direction how to do that? Thanks.

6 Comments

You should keep it to 1 question per thread. Makes it difficult to answer and accept.
Regarding the legend question: you're adding a legend item outside the if/end, and without regard to your i loop. This leads to having 5 legend items all based on i = 31.
I found some help with my legend and modified the code as follows and obtained the attached figure. Could anyone help me with getting the lines instead of boxes in the legend? Thanks.
figure(1)
colorspec1 = colormap(jet(5));
for day = 1:31
icolor = MA(day,2);
semilogx(All_112(:,1), All_112(:,day+1),'-', 'Linewidth', 2, 'Color', colorspec1(icolor,:));
hold all
end
for ws = 1:size(colorspec1,1)
p(ws) = patch(NaN, NaN, colorspec1(ws,:));
end
lbl = {'1','2','3','4','5'};
legend(p, lbl);
xlim([0.1 2.5]);
ylim([0 0.000035]);
hold off
Replace patch with plot. But that's a very messy way to do it. When I find some time not on my phone I'll help you out.
No worries. Thank you very much for your help. It works with plot.
My updated answer should better solve the down-selected legend. There's a chance it isn't compatible with your release of MATLAB; I'm not sure when the behavior of legend I'm taking advantage of was added.

Sign in to comment.

 Accepted Answer

There's no reason to loop over colors - you already have the index in MA(i,2). Assuming the second column of MA is restricted to 1:5, use this:
colorspec1 = colormap(jet(5));
hline = gobjects(31,1);
for ii = 1:31
icolor = MA(ii,2);
hline(ij) = semilogx(All_112(:,1),All_112(:,ii+1),'-', ...
'Linewidth',2,'Color',colorspec1(icolor,:), ...
'DisplayName',sprintf('Wind speed = %d',icolor));
hold all
end
[~,inds] = unique(MA(:,2));
legend(hline(inds));

6 Comments

I don't recommend using i and j as loop indices. It is much cleaner to use something meaningful (like icolor) if you're looping over colors. Since I couldn't determine what your loop actually is, I went with ii (I also like lp for "loop").
Hi Greg, thanks a lot for your help. I am pretty new to programming and will keep in mind your suggestions for a cleaner code. I tried running the code by incorporating your suggestions and this solves my first issue. I am sorry for asking two questions regarding the code and will not do so in future. Regarding the legend, I am still getting it as five lines with first five colors in the loop, instead of 5 different colored lines for 5 values of MA(:,2), please see the attached figure. Thanks.
Sorry Greg, still getting the same legend.
Hi Greg, this works perfectly. Thanks a lot.

Sign in to comment.

More Answers (0)

Asked:

on 10 Feb 2018

Commented:

on 13 Feb 2018

Community Treasure Hunt

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

Start Hunting!