How to plot multiple days of data overlayed on one plot and ignoring the date

13 次查看(过去 30 天)
I have a numerical array consisting of 2 columns representing a data set. I have x-values which are datenum which I have converted to timeofday values, resulting in the following columns:
Column 1: timeofday values corresponding to a series of time values over 10 days.
Column 2: magnitude values.
How do I go about plotting values for each day overlayed as separate plots with different colours?? (all of day one is blue, all of day two is green, etc.)
I imagine a while loop is required which states that while the date is a, plot b and c, then hold on and repeat?? I'm sure it will be simple but I'm stumped!

回答(1 个)

Arjun
Arjun 2024-11-4,5:50
Hi @Jaye,
I understand that you have data for different days, and you want to plot the hourly activities of each day using different color.
To visualize hourly activities for different days, you can utilize a “for” loop to iterate through each unique day and plot the corresponding data with distinct colors. Start by using the “unique” function on your date values to determine the number of unique days present in your dataset. Once you have identified the unique days, generate a distinct color for each day using a colormap like “lines”. Then, employ the “hold on” command to overlay all the data on a single graph. Within the “for” loop, plot the data for each day, ensuring each set of data is assigned a unique color.
Kindly refer to the code below for better understanding:
% Generate some dummy data
datenumValues = datenum('2023-10-01') + (0:0.1:9.9)';
timeofday = mod(datenumValues, 1) * 24;
magnitude = rand(length(datenumValues), 1) * 100;
data = [timeofday, magnitude];
% Find unique dates
uniqueDays = unique(floor(datenumValues));
% Number of colors required
colors = lines(length(uniqueDays));
figure;
% For displaying on the same graph
hold on;
for i = 1:length(uniqueDays)
dayIndices = floor(datenumValues) == uniqueDays(i);
plot(data(dayIndices, 1), data(dayIndices, 2), 'Color', colors(i, :), 'DisplayName', datestr(uniqueDays(i), 'yyyy-mm-dd'));
end
% Add details to the plot
xlabel('Time of Day (hours)');
ylabel('Magnitude');
title('Magnitude vs. Time of Day for Each Day');
legend('show');
grid on;
Kindly refer to the documentation of “unique”, “lines” and “hold” for better understanding:
I hope this will help!

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by