How can I use a legend for only only line in a subpot?
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!