Plot label with text function
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Star Strider
on 28 Dec 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 Comments
More Answers (1)
the cyclist
on 27 Dec 2023
Edited: the cyclist
on 27 Dec 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.
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!