MATLAB Answers

legend in loop for data and fit

4 views (last 30 days)
laurent jalabert
laurent jalabert on 13 Oct 2019
dear all,
I am trying to create a legend using a for loop and plotting both data and fit for each set.
So I have alternative "data" and "fit" on my plot with the corresponding colors.
However, I failed to output the correct legend. In my legend, I would like the "data1" to be a value that depends on index fr (loop), and I would like to display the coeeficients {a,b} of the fit for each set of data.
MMarkers = {'o','s','p','h','v','^','d','o','s','p','h','v','^','d'};
markersColor = {[0 0.4470 0.7410], [0.8500 0.3250 0.0980], [0.9290 0.6940 0.1250],[0.4940 0.1840 0.5560],...
[0.4660 0.6740 0.1880], [0.3010 0.7450 0.9330], [0.6350 0.0780 0.1840],...
[0 0.4470 0.7410], [0.8500 0.3250 0.0980], [0.9290 0.6940 0.1250],[0.4940 0.1840 0.5560],...
[0.4660 0.6740 0.1880], [0.3010 0.7450 0.9330], [0.6350 0.0780 0.1840]};
colorsF = parula(4+2); %legend on Frequency
long = 800; large = 800;
fig(1) = figure('PaperUnits','inches','PaperType','A4','PaperOrientation','landscape','Color',[1 1 1], 'OuterPosition',[50 1 800 800]);
for fr=1:4 % legende
markerColour = colorsF(fr,:);% colors
x = (1:1:10);
y = 2.*x+fr;
err_y=0.01.*y;
h(:,fr)=errorbar(x,y,err_y,'-o','Color',markerColour,'MarkerEdgeColor',markerColour,...
'MarkerFaceColor',markerColour,'MarkerSize',14,'LineWidth',2);
set(gca,'Xscale','lin','Yscale','lin','FontSize',24);
hold on
f=ezfit(x,y,'Y(X) = a*X+b');
showfit(f,'fitcolor',markerColour,'fitlinestyle','--');
hold on
grid on
end
xlabel('X'); ylabel('Y');
title('my title','FontSize',16);
legend
I would like this code to work for any number of loops (not being restrictive to fr=4 at max). This is because inn this example, I show 4 data/fit sets, but usually, I have about 20 plots.

  0 Comments

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by