putting a legend to plot a data in a for loop

조회 수: 1 (최근 30일)
Meva
Meva 2015년 6월 2일
편집: Cindy Solomon 2015년 6월 3일
Hello,
I have :
if l==1
thickness = bodyfunction1;
end
if l==2
thickness = bodyfunction2;
end
if l==3
thickness = bodyfunction3;
end
if l==4
thickness =bodyfunction4;
end
...... I want to plot a data with indexes from i=1 to 4:
for i=1:4
plot(t_vect(1:t_clashing_index(i)-20), H1Rarray(1:t_clashing_index(i)-20, i));
hold on;
legend('with body shape =',num2str(i),...'location','Best');
plot(t_vect(1:t_clashing_index(i)-20), H1Larray(1:t_clashing_index(i)-20, i));
hold on;
end
The question is : How can I distinguish H1Rarray and H1Larray with four different body thickness functions?
How can I put legend so that the program can understand if i=1 H1Rarray shows this is for bodyfunction1, if i=2 H1Rarray shows this is for bodyfunction2 , ... and the same thing for H1Larray ??
Many thanks
  댓글 수: 1
Sriram Narayanan
Sriram Narayanan 2015년 6월 3일
The two sections of code that you have shared is not clear enough. You are assigning the "thickness" variable to the corresponding body function, but, are plotting t_clashing_index.
I would have to look at the code structure, but, assuming that the four body functions are MATLAB functions that return the body thickness function, you could have a function that accepts the desired value of "l" as input and within that function, the for-loop could immediately follow the if statement once the desired value of "l" has been selected by the user.

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

답변 (1개)

Cindy Solomon
Cindy Solomon 2015년 6월 3일
편집: Cindy Solomon 2015년 6월 3일
Hi Meva,
Could you please clarify what your desired outcome would be? I wasn't quite sure if you wanted 8 legend entries that would specify something like "H1RArray, BodyStyle X" or if you only wanted 4 legend entries (one for each "body thickness") with some other way of distinguishing between the two arrays without having an entry in the legend, or something else entirely.
If you wanted the 4 thicknesses to have a different plot style, you could change the call to your plot to be something like:
plot(t_vect(1:t_clashing_index(i)-20),H1Rarray(1:t_clashing_index(i)-20, plotStyle{i})
such that there would be a unique color/marker combination for each of the 4 thicknesses that you defined earlier. Ex:
plotStyle = {'b','k','r.','g0', ...};
Within your for loop, you could then add something like:
legendInfo{i} = ['Body Shape = ' num2str(i)];
and then outside the loop add the command:
legend(legendInfo)
Is there a reason why you have to use a 'for' loop? Vectorizing your code would be more efficient, and you could more easily do something like the example below that shows different colors for each thickness and different marker types for each array. However, in doing so, the color of the marker would not be the same as any of the body thicknesses, so this might be confusing for an observer.
ax = gca;
xData = plot(magic(4),'Marker','o');
hold on
ax.ColorOrderIndex = 1;
yData = plot(magic(4)+5,'Marker','.');
ax.ColorOrderIndex = 1;
pColors = plot(1,[1 1 1 1]);
pStyles(1) = plot(1,1,'o');
pStyles(2) = plot(1,1,'.');
legend([pColors; pStyles'],{'Color 1','Color 2','Color 3','Color 4','Style 1','Style 2'});
set([pColors; pStyles'],'Visible','off');
Hope this helps!

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by