How to includ/plot the local maxima date on a plot?

조회 수: 5 (최근 30일)
Wolfgang McCormack
Wolfgang McCormack 2021년 1월 28일
답변: Suraj Kumar 2025년 3월 27일
Hi everyone, I have defined the following and it shows the date values intractively (mytime) on the plot, but I want to actually plot them on the figure so that I can export it as jpeg. Also, how can I highlight them with triangles as the normal findpeaks does?
[pks,locs] = findpeaks(Data,'MinPeakProminence',2)
x_peaks = Mytime(locs)
plot(Mytime,Data, x_peaks,pks,'pg')

답변 (1개)

Suraj Kumar
Suraj Kumar 2025년 3월 27일
Hi Wolfgang,
From what I understand you want to plot the local maxima, annotate them with their corresponding dates and highlight these maxima using triangles.
You can use "findpeaks" functon to identify the local maxima in your data. Then you can plot the entire dataset to provide context and use "plot" function to overlay the triangles at peak locations to highlight them.You can refer to the attached code snippets with sample data for better understanding:
Data = [1, 3, 7, 1, 2, 6, 0, 1, 3, 2, 5, 1];
Mytime = datetime(2023,10,1) + days(0:length(Data)-1);
[pks, locs] = findpeaks(Data, 'MinPeakProminence', 2);
x_peaks = Mytime(locs);
plot(Mytime, Data, '-b');
hold on;
plot(x_peaks, pks, '^r', 'MarkerFaceColor', 'r');
Then you can use the "text" function to annotate each peak with corresponding date and use the "saveas" function to export the plot as a JPEG image.
for i = 1:length(locs)
text(x_peaks(i), pks(i), datestr(x_peaks(i), 'mm/dd'), 'VerticalAlignment', 'bottom', 'HorizontalAlignment', 'right');
end
xlabel('Date');
ylabel('Data Value');
title('Data with Local Maxima Highlighted');
datetick('x', 'mmm dd', 'keepticks');
saveas(gcf, 'plot_with_peaks.jpg');
hold off;
To learn more about the functions used above, you can refer to the following documentation links:
Happy Coding!

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by