Plot label with text function

5 次查看(过去 30 天)
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.

采纳的回答

Star Strider
Star Strider 2023-12-28
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 个评论
Doyinsola
Doyinsola 2023-12-28
Thank you. This really solved my many trial and errors with ease. Thank you once again.

请先登录,再进行评论。

更多回答(1 个)

the cyclist
the cyclist 2023-12-27
编辑:the cyclist 2023-12-27
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 个评论
Doyinsola
Doyinsola 2023-12-28
Thank you for your attempt and help. I have been able to solve the problem with the code shared by star strider.

请先登录,再进行评论。

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by