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

7 visualizaciones (últimos 30 días)
Wolfgang McCormack
Wolfgang McCormack el 28 de En. de 2021
Respondida: Suraj Kumar el 27 de Mzo. de 2025
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')

Respuestas (1)

Suraj Kumar
Suraj Kumar el 27 de Mzo. de 2025
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!

Productos


Versión

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by