Plot label with text function

5 visualizaciones (últimos 30 días)
Doyinsola
Doyinsola el 27 de Dic. de 2023
Comentada: Star Strider el 28 de Dic. de 2023
Hi everyone, please help debug the error in the code below
I made a plot in which I added a text to each peaks (peak assignment with label) but the alignment of the text are not well placed accordingly. See below my script and the plot generated with the script. I want the text label to be close to each of the peaks identified. I also attached the data I plotted. Thanks
NB: 'Si_layer1_ExpN' is my experimental data which represent intensity, 'wv1' is the wavelenght of my experimental data and 'matched data' (struct) is the result of the comparison I did with my experimental data using a database.
script
figure;
plot(wv1, Si_layer1_ExpN, 'b', 'LineWidth', 1);
hold on
for i = 1:length(matched_data)
plot(matched_data(i).wavelength, matched_data(i).intensity, 'ko', 'MarkerSize', 5);
text(matched_data(i).wavelength, matched_data(i).intensity, matched_data(i).labels, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left', 'Color', 'k');
end
xlabel('Wavelength (nm)');
ylabel('Intensity');
legend('Experimental Data', 'Matched Peaks');
The script return first plot below
I want the peak labelling to look like the below plot. Labels placed close to each peaks.

Respuesta aceptada

Star Strider
Star Strider el 28 de Dic. de 2023
All the ‘matched_data.intensity’ entries are all set to 1, so they all plot at 1 rather than the peak values. A way to correct for that is to add this interp1 call to the loop before the others:
matched_data(i).intensity = interp1(wv1, Si_layer1_ExpN, matched_data(i).wavelength);
That interpolates the correct values, based on the ‘matched_data.wavelength’ values. No other changes to your code are necessary.
Try this —
load('matlab_data.mat')
% whos
figure;
plot(wv1, Si_layer1_ExpN, 'b', 'LineWidth', 1);
hold on
for i = 1:length(matched_data)
matched_data(i).intensity = interp1(wv1, Si_layer1_ExpN, matched_data(i).wavelength);
plot(matched_data(i).wavelength, matched_data(i).intensity, 'ko', 'MarkerSize', 5);
text(matched_data(i).wavelength, matched_data(i).intensity, matched_data(i).labels, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left', 'Color', 'k');
end
xlabel('Wavelength (nm)');
ylabel('Intensity');
legend('Experimental Data', 'Matched Peaks');
.
  2 comentarios
Doyinsola
Doyinsola el 28 de Dic. de 2023
Thank you. This really solved my many trial and errors with ease. Thank you once again.
Star Strider
Star Strider el 28 de Dic. de 2023
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

the cyclist
the cyclist el 27 de Dic. de 2023
Editada: the cyclist el 27 de Dic. de 2023
The intensities in your structure do not line up with the variable Si_layer1_ExpN.
I show this below by splitting figure into two subplots, and making your plot markers bigger:
load("matlab_data.mat")
figure
tiledlayout(2,1)
nexttile
plot(wv1, Si_layer1_ExpN, 'b', 'LineWidth', 1);
nexttile
hold on
for i = 1:length(matched_data)
plot(matched_data(i).wavelength, matched_data(i).intensity, 'ko', 'MarkerSize', 8);
text(matched_data(i).wavelength, matched_data(i).intensity, matched_data(i).labels, 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'left', 'Color', 'k');
end
xlabel('Wavelength (nm)');
ylabel('Intensity');
Notice that the two y-axes have very different ranges.
I'm not sure how you want to solve that, so I'll leave it at that.
  1 comentario
Doyinsola
Doyinsola el 28 de Dic. de 2023
Thank you for your attempt and help. I have been able to solve the problem with the code shared by star strider.

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by