Add text to plot using a loop

6 visualizaciones (últimos 30 días)
Rosalie Tostevin
Rosalie Tostevin el 22 de Ag. de 2023
Comentada: Rosalie Tostevin el 22 de Ag. de 2023
I am plotting FTIR data. The code below should work, but I have substituted in randomly generated data.
I'd like the text function in the loop to produce a label for each plot. i.e., p1(1) will be labelled pre-sorption, and p1(5) will be labelled 'SiO2 = 2 mM'. Currently, the code produces text in the right position, but all five labels appear at all five locations. How do I modify the code to make sure each label appears sequentially?
Thanks!
AM_cm=([400:1:4000]);
AM_cm=AM_cm.';
AM=([rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1)]);
figure(1);
set(gca,'Xdir','reverse');
set(gca,'ytick',[]); set(gca,'yticklabel',[]);
xlabel ('cm-1','Fontsize', 14); ylabel ('Absorbance','Fontsize', 14);
for a = 1:5% FTIR data
hold on; box on;
p1(a)=plot(AM_cm,AM(:,a),'-','LineWidth',2);
text(3900, AM(1,a),['pre-sorption','SiO2= 0.5 mM','SiO2 = 1 mM','SiO2 = 1.5 mM','SiO2 = 2 mM'],'Fontsize', 12);
end
  3 comentarios
Dyuman Joshi
Dyuman Joshi el 22 de Ag. de 2023
Do you want a legend?
Rosalie Tostevin
Rosalie Tostevin el 22 de Ag. de 2023
I'd rather label the lines than have a legend, because they are all the same colour.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Ag. de 2023
AM_cm=([400:1:4000]);
AM_cm=AM_cm.';
AM=([rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1)]);
figure(1);
set(gca,'Xdir','reverse');
set(gca,'ytick',[]); set(gca,'yticklabel',[]);
xlabel ('cm-1','Fontsize', 14); ylabel ('Absorbance','Fontsize', 14);
labels = {'pre-sorption','SiO2= 0.5 mM','SiO2 = 1 mM','SiO2 = 1.5 mM','SiO2 = 2 mM'};
for a = 1:length(labels)% FTIR data
hold on; box on;
p1(a)=plot(AM_cm,AM(:,a),'-','LineWidth',2);
text(3900, AM(1,a),labels{a},'Fontsize', 12);
end

Más respuestas (0)

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by