How do I use only one legend entry for sets of xlines?
12 次查看(过去 30 天)
显示 更早的评论
I am currently trying to create a plot with two sets of xlines, with one set colored red and the other colored green. For some reason, when I plot them, the legend entry shows that both lines are identified as green. I'm not sure how to go about fixing this issue, so any help would be greatly appreciated! Attached is the code I'm using as well as an image with the problem.
for trialNumber = 1:length(filenameDataAC)
legendNames = {"Accelerometers","Motion Capture","Upper Threshold (UT)","Lower Threshold (LT)","Stim On","Stim Off"};
fig = figure('Name', dataStoreACFTrunc{trialNumber,1});
% Pitch (NNP+VIC)
subplot(3,1,1)
plot(dataStoreACFTrunc{trialNumber,2}.time, dataStoreACFTrunc{trialNumber,2}.Pitch)
hold on
plot(intDataStoreVICFTrunc{trialNumber,2}.time, intDataStoreVICFTrunc{trialNumber,2}.Pitch)
set(gca, 'FontSize', 16, 'FontWeight', 'bold')
xlabel('Time (s)', 'FontSize', 18)
ylabel('Pitch Angle (deg)', 'FontSize', 18)
yline(pUTs(trialNumber),'-','UT','LabelHorizontalAlignment','left')
yline(pLTs(trialNumber),'-','LT','LabelHorizontalAlignment','left')
x1 = xline(dataStoreACFTrunc{trialNumber,2}.time(stimOn{trialNumber}),'--','Color','g');
x2 = xline(dataStoreACFTrunc{trialNumber,2}.time(stimOff{trialNumber}),'--','Color','r');
ylim([-60,60])
yticks([-60,-30,0,30,60])
xlim([0 dataStoreACFTrunc{trialNumber,2}.time(end)])
legend(legendNames)
% Roll (NNP+VIC)
subplot(3,1,2)
plot(dataStoreACFTrunc{trialNumber,2}.time, dataStoreACFTrunc{trialNumber,2}.Roll)
hold on
plot(intDataStoreVICFTrunc{trialNumber,2}.time, intDataStoreVICFTrunc{trialNumber,2}.Roll)
set(gca, 'FontSize', 16, 'FontWeight', 'bold')
xlabel('Time (s)', 'FontSize', 18)
ylabel('Roll Angle (deg)', 'FontSize', 18)
yline(rUTs(trialNumber),'-','UT','LabelHorizontalAlignment','left')
yline(rLTs(trialNumber),'-','LT','LabelHorizontalAlignment','left')
xline(dataStoreACFTrunc{trialNumber,2}.time(stimOn{trialNumber}),'--','Color','g')
xline(dataStoreACFTrunc{trialNumber,2}.time(stimOff{trialNumber}),'--','Color','r')
ylim([-60,60])
yticks([-60,-30,0,30,60])
xlim([0 dataStoreACFTrunc{trialNumber,2}.time(end)])
% Controller Stim State
subplot(3,1,3)
plot(dataStoreACFTrunc{trialNumber,2}.time, dataStoreACFTrunc{trialNumber,2}.StimState)
set(gca, 'FontSize', 16, 'FontWeight', 'bold')
xlabel('Time (s)', 'FontSize', 18)
ylabel('Stim State', 'FontSize', 18)
xline(dataStoreACFTrunc{trialNumber,2}.time(stimOn{trialNumber}),'--','Color','g')
xline(dataStoreACFTrunc{trialNumber,2}.time(stimOff{trialNumber}),'--','Color','r')
ylim([0,5])
xlim([0 dataStoreACFTrunc{trialNumber,2}.time(end)])
sgtitle('Return to Upright Controller Outputs', 'FontSize', 32, 'FontWeight', 'bold')
% saveas(fig, dataStoreAC{trialNumber,1},'fig')
% figsPath = dir(fullfile(currentPath,'*.fig'));
% movefile(fullfile(figsPath.folder,figsPath.name),saveFigPath);
end
Thank you!
0 个评论
采纳的回答
Voss
2024-5-14
One way is to explicitly state which line objects to put in the legend, in the call to legend. For example:
subplot(3,1,1)
h1 = plot(dataStoreACFTrunc{trialNumber,2}.time, dataStoreACFTrunc{trialNumber,2}.Pitch);
hold on
h2 = plot(intDataStoreVICFTrunc{trialNumber,2}.time, intDataStoreVICFTrunc{trialNumber,2}.Pitch);
set(gca, 'FontSize', 16, 'FontWeight', 'bold')
xlabel('Time (s)', 'FontSize', 18)
ylabel('Pitch Angle (deg)', 'FontSize', 18)
y1 = yline(pUTs(trialNumber),'-','UT','LabelHorizontalAlignment','left');
y2 = yline(pLTs(trialNumber),'-','LT','LabelHorizontalAlignment','left');
x1 = xline(dataStoreACFTrunc{trialNumber,2}.time(stimOn{trialNumber}),'--','Color','g');
x2 = xline(dataStoreACFTrunc{trialNumber,2}.time(stimOff{trialNumber}),'--','Color','r');
ylim([-60,60])
yticks([-60,-30,0,30,60])
xlim([0 dataStoreACFTrunc{trialNumber,2}.time(end)])
legend([h1(1),h2(1),y1(1),y2(1),x1(1),x2(1)],legendNames)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Title 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!