필터 지우기
필터 지우기

How can I use a legend for only only line in a subpot?

조회 수: 1 (최근 30일)
Iris Hagoort
Iris Hagoort 2018년 5월 3일
답변: Julian Hapke 2018년 5월 3일
My struct contains 4 conditions and 4 bins. Every subplot is one bin and in every subplot there are lines for all four conditions. In the legend I want to show the four conditions. The script below worked fine when I only plotted this line:
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) , 'color' , myCol(k,:));
However, I also want to plot mean+standard deviation and mean-standard deviation. The legend is not correct anymore. Does anyone know how to change it?
figure;
myCol = [1 0 0; 0 1 0; 0 0 1; 1 0 1 ];
for l = 1:length(namesBins);
subplot(2,2,l);
for k = 1:length(namesConditions);
hold on;
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) , 'color' , myCol(k,:));
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) + ...
StdInSecondsAllParticipants.(namesConditions{k}).(namesBins{l}), ':', 'color' , myCol(k,:));
plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) - ...
StdInSecondsAllParticipants.(namesConditions{k}).(namesBins{l}), ':', 'color' , myCol(k,:));
end
legend(namesConditions)
title(namesBins{l});
xlabel('Time (s)')
ylabel('Concentration OxyHb (M/l)')
end

답변 (1개)

Julian Hapke
Julian Hapke 2018년 5월 3일
the legend is correct for what you plotted, each iteration creates 3 plots, the namesConditions has 4 entries, which are assigned to the first 4 plots matlab finds in the axes, so your first plot has the correct entry and the next two do not have the entries you want.
You could plot mean and std after creating the legend, to that the 4 conditions are plotted first and linked to the 4 legend entries you are creating.
Or you could use the
legend(subset,___)
syntax of the command and pass the plot handles of the condition plots into the call by collecting them in the loop.
p(k) = plot(Time',InSecondsAllParticipants.(namesConditions{k}).(namesBins{l}) , 'color' , myCol(k,:));
and
legend(p,namesConditions);

카테고리

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