Why missing color in legend

5 次查看(过去 30 天)
Dear All,
I was create code for plotting the data. But I do not know why in my legend the color do not folow as graphh. There are missing color yellow, black and magenta.
all the data i was attached here
clc
close all
clear all
LD = load('xstats.mat'); %xstats.mat can get from imtool3D
xstats = LD.xstats
LD = load('ystats.mat'); %ystats.mat can get from imtool3D
ystats = LD.ystats
F = openfig('L1.fig');
Lines = findobj(F, 'Type','Line');
x = Lines.XData;
y = Lines.YData;
wx = fwhm(x,y)
[wm,xr,hm] = myFWHM(x,y)
[ymax,idx] = max(y);
ymin = min(y);
hold on
plot(x, y)
plot(xr, [1 1]*hm+ymin, '.-r')
hold off
LD1 = load('xstats1.mat'); %xstats.mat can get from imtool3D
xstats1 = LD1.xstats1
LD1 = load('ystats1.mat'); %ystats.mat can get from imtool3D
ystats1 = LD1.ystats1
F1 = openfig('L2.fig');
Lines1 = findobj(F1, 'Type','Line');
x1 = Lines1.XData;
y1 = Lines1.YData;
wx1 = fwhm(x1,y1)
[wm1,xr1,hm1] = myFWHM(x1,y1)
[ymax1,idx1] = max(y1);
ymin1 = min(y1);
LD2 = load('xstats2.mat'); %xstats.mat can get from imtool3D
xstats2 = LD2.xstats2
LD2 = load('ystats2.mat'); %ystats.mat can get from imtool3D
ystats2 = LD2.ystats2
F2 = openfig('L3.fig');
Lines2 = findobj(F2, 'Type','Line');
x2 = Lines2.XData;
y2 = Lines2.YData;
wx2 = fwhm(x2,y2)
[wm2,xr2,hm2] = myFWHM(x2,y2)
[ymax2,idx2] = max(y2);
ymin2 = min(y2);
LD3 = load('xstats3.mat'); %xstats.mat can get from imtool3D
xstats3 = LD3.xstats3
LD3 = load('ystats3.mat'); %ystats.mat can get from imtool3D
ystats3 = LD3.ystats3
F3 = openfig('L4.fig');
Lines3 = findobj(F3, 'Type','Line');
x3 = Lines3.XData;
y3 = Lines3.YData;
wx3 = fwhm(x3,y3)
[wm3,xr3,hm3] = myFWHM(x3,y3)
[ymax3,idx3] = max(y3);
ymin3 = min(y3);
LD4 = load('xstats4.mat'); %xstats.mat can get from imtool3D
xstats4 = LD4.xstats4
LD4 = load('ystats4.mat'); %ystats.mat can get from imtool3D
ystats4 = LD4.ystats4
F4 = openfig('L5.fig');
Lines4 = findobj(F4, 'Type','Line');
x4 = Lines4.XData;
y4 = Lines4.YData;
wx4 = fwhm(x4,y4)
[wm4,xr4,hm4] = myFWHM(x4,y4)
[ymax4,idx4] = max(y4);
ymin4 = min(y4);
LD5 = load('xstats5.mat'); %xstats.mat can get from imtool3D
xstats5 = LD5.xstats5
LD5 = load('ystats5.mat'); %ystats.mat can get from imtool3D
ystats5 = LD5.ystats5
F5 = openfig('L6.fig');
Lines5 = findobj(F5, 'Type','Line');
x5 = Lines5.XData;
y5 = Lines5.YData;
wx5 = fwhm(x5,y5)
[wm5,xr5,hm5] = myFWHM(x5,y5)
[ymax5,idx5] = max(y5);
ymin5 = min(y5);
hold on
plot(x, y,"", "Linestyle", "--","LineWidth",1,"Color","b")
plot(xr, [1 1]*hm+ymin, '.-b')
text(x(idx), hm+ymin, sprintf(' %.3f',wm), 'Horiz','center', 'Vert','bottom')
plot(x1, y1,"LineStyle","--","LineWidth",1,"Color","g" )
plot(xr1, [1 1]*hm1+ymin1, '.-g')
text(x1(idx1), hm1+ymin1, sprintf(' %.3f',wm1), 'Horiz','center', 'Vert','bottom')
plot(x2, y2,"LineStyle","--","LineWidth",1,"Color","r" )
plot(xr2, [1 1]*hm2+ymin2, '.-r')
text(x2(idx2), hm2+ymin2, sprintf(' %.3f',wm2), 'Horiz','left', 'Vert','bottom')
plot(x3, y3,"LineStyle","--","LineWidth",1,"Color","black" )
plot(xr3, [1 1]*hm3+ymin3, '.-black')
text(x3(idx3), hm3+ymin3, sprintf(' %.3f',wm3), 'Horiz','center', 'Vert','bottom')
plot(x4, y4,"LineStyle","--","LineWidth",1,"Color","yellow" )
plot(xr4, [1 1]*hm4+ymin4, '.-yellow')
text(x4(idx4), hm4+ymin4, sprintf(' %.3f',wm4), 'Horiz','center', 'Vert','bottom')
plot(x5, y5,"LineStyle","--","LineWidth",1,"Color","magenta" )
plot(xr5, [1 1]*hm5+ymin5, '.-magenta')
text(x5(idx5), hm5+ymin5, sprintf(' %.3f',wm5), 'Horiz','center', 'Vert','bottom')
hold off
grid
legend('FWHM Line Source 1','FWHM Line Source 2','FWHM Line Source 3','FWHM Line Source 4','FWHM Line Source 5','FWHM Line Source 6')
  3 个评论
mohd akmal masud
mohd akmal masud 2024-10-15
You can download as in folder attached.

请先登录,再进行评论。

采纳的回答

Manish
Manish 2024-10-15
编辑:Manish 2024-10-15
Hi,
I understand that you want the legends to match the graphs.
To achieve this, ensure that you save the plot handles(h1,h2,h3,h4) in variables, which you can then use in the 'legend' function.
Here is the code sample for 4 plots:
hold on;
h1 = plot(x1, y1, "LineStyle", "--", "LineWidth", 1, "Color", "g");
plot(xr1, [1 1]*hm1 + ymin1, '.-g');
text(x1(idx1), hm1 + ymin1, sprintf(' %.3f', wm1), 'Horiz', 'center', 'Vert', 'bottom');
h2 = plot(x2, y2, "LineStyle", "--", "LineWidth", 1, "Color", "r");
plot(xr2, [1 1]*hm2 + ymin2, '.-r');
text(x2(idx2), hm2 + ymin2, sprintf(' %.3f', wm2), 'Horiz', 'left', 'Vert', 'bottom');
h3 = plot(x3, y3, "LineStyle", "--", "LineWidth", 1, "Color", "black");
plot(xr3, [1 1]*hm3 + ymin3, '.-black');
text(x3(idx3), hm3 + ymin3, sprintf(' %.3f', wm3), 'Horiz', 'center', 'Vert', 'bottom');
h4 = plot(x4, y4, "LineStyle", "--", "LineWidth", 1, "Color", "yellow");
plot(xr4, [1 1]*hm4 + ymin4, '.-yellow');
text(x4(idx4), hm4 + ymin4, sprintf(' %.3f', wm4), 'Horiz', 'center', 'Vert', 'bottom');
hold off;
grid on;
% Add legend for the primary lines using handles
legend([h1, h2, h3, h4], 'FWHM Line Source 1', 'FWHM Line Source 2', 'FWHM Line Source 3', 'FWHM Line Source 4');
Hope this solves!

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by