Plot label with text function

5 ビュー (過去 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 Center および File ExchangeDescriptive Statistics についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by