What is wrong with my legend?

2 次查看(过去 30 天)
Stwiwi
Stwiwi 2018-10-31
Hey people,
So I have this Code
% linestyle = {'-' '--' ':'};
color = {'b' 'k' 'y'};
force_index = {'X' 'Y' 'Z'};
TITLE = {'Test 008-2 - floating buoy';'Test 033-1 - submerged buoy'};
test_no = {'Test_008_2';'Test_033_1'};
perc_Matrix = ones(3,2);
% Plot Ratios
for t=1:2
figure
set(gcf, 'Visible', 'off')
box on
hold on
for d=1:3
F_XX = Force(t).Tests(d).Data;
time_XX = Force(t).Tests(5).Data;
Resultant_XX = Force(t).Tests(4).Data;
time_N = normalize(time_XX,'range');
yaxis = abs(F_XX)./Resultant_XX;
yaxis = sortrows(yaxis);
plot(time_N,yaxis,color{d},'LineWidth',2)
ylabel('F/F_R [-]')
xlabel('Normalized sample range [-]')
xmin = 0;
xmax = 1;
xlim([xmin xmax])
% Calculate percentiles
semilogy(time_N,yaxis);
if t==1
y = 0.9;
elseif t==2
y = 0.5;
end
percentile = interp1(yaxis,time_N,y);
perc_Matrix(d,t) = 100-ceil(100*percentile);
line([0 percentile],[y y],'Color','black','Linewidth',0.01,'LineStyle',...
':','HandleVisibility','off');
line([percentile percentile],[0 y],'Color','black','Linewidth',0.1,'LineStyle',...
':','HandleVisibility','off');
lgd = ['F_' force_index{d} '/F_R'];
legendInfo{d} = (lgd);
end
legend(legendInfo,'Location','northwest')
orient(gcf,'portrait')
set(gcf,'PaperPosition',[0 0 47 24])
set(findall(gcf,'-property','FontSize'),'FontSize',16)
tit = [char(TITLE(t)) ' - Ratios of Forces X,Y,Z to Resultant'];
title(tit,'FontSize',18)
set(findall(gcf,'-property','FontName'),'FontName','SansSerif')
saveas(gcf,[char(test_no(t)) '.tif']);
close
end
When I run it everything is fine except the legend in my tif-file (also in mat figure). The colors in the legend are just not right and I have no idea why. It looks like this:
Fz/Fr should be yellow and Fy/Fr should be black. Only Fx/Fr is right. I attached the used Force structure.
Maybe anybody can help me?:)
Thank you

回答(1 个)

Prateekshya
Prateekshya 2024-9-4
Hello @Stwiwi,
The issue with the legend colors not matching the plotted lines in your MATLAB code could be due to the legend not associating each entry with the corresponding plot. Here is a sample code which you can utilize to modify your code:
% Sample data
time = linspace(0, 1, 100); % Normalized time from 0 to 1
data1 = sin(2 * pi * time); % Sample data for X
data2 = cos(2 * pi * time); % Sample data for Y
data3 = sin(2 * pi * time + pi/4); % Sample data for Z
% Colors for each plot
colors = {'b', 'k', 'y'};
labels = {'Data X', 'Data Y', 'Data Z'};
% Create figure
figure;
hold on;
% Plot each dataset and store plot handles
plotHandles = gobjects(1, 3); % Preallocate plot handles
plotHandles(1) = plot(time, data1, 'Color', colors{1}, 'LineWidth', 2);
plotHandles(2) = plot(time, data2, 'Color', colors{2}, 'LineWidth', 2);
plotHandles(3) = plot(time, data3, 'Color', colors{3}, 'LineWidth', 2);
% Add labels
xlabel('Normalized Time');
ylabel('Amplitude');
title('Sample Data Plot');
% Create legend using plot handles
legend(plotHandles, labels, 'Location', 'northwest');
% Display the plot
hold off;
After running this code, the below figure is obtained as the output. In this figure, the legends are matched with the respective plots correctly.
For more information on legends, please follow the below link:
I 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