Plot label with text function

조회 수: 6 (최근 30일)
Doyinsola
Doyinsola 2023년 12월 27일
댓글: Star Strider 2023년 12월 28일
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.
Star Strider
Star Strider 2023년 12월 28일
As always, my pleasure!

댓글을 달려면 로그인하십시오.

추가 답변 (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에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by