Plot imagesc on Discontinuous x-Axis with datenum
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have an imagesc to plot with distance on y axis, z in colorbar, and datenum on x axis. Y and Z are in the matrix (120 x 500) and datetime is in seperate vector t (120 x 1).
I have the following code to plot the imagesc and t, but t only covers data between 6am and 6pm.
How could it make a break in the plot with only showing data for 12 hours with then a break in for example dashed line?
Also, could it be possible to only show the day with the start time and end time (so no label for every xtick)?
figure
tnum = datenum(t);
imagesc(D', 'Interpolation', 'bilinear', 'XData', tnum);
set(gca, 'YDir', 'normal');
colorbar
xticks(tnum);
datetick('x', 'HH:MM', 'keepticks')
set(gca, 'XTickLabelRotation', 45)
0 Commenti
Risposte (1)
Divyam
il 9 Set 2024
To make a break in your plot for the desired time range, you can create a logical mask for the array "t" and filter out the time data for the data in the "datetime" vector lying in the range "6 A.M. to 6 P.M." and then plot the entire data.
For removing the excess "xtick" labels you can use the "xtick" functions to first enter the "datetime" data which has to be labelled and then create "xticklabels" for the same.
startTime = datetime('today', 'Format', 'HH:mm') + hours(6); % 6 AM today
endTime = datetime('today', 'Format', 'HH:mm') + hours(18); % 6 PM today
endTimeT= endTime + minutes(300); % Adding more entries to the "t" vector to show break in the plot
% Create a vector of datetime objects for 6 AM to 12 midnight with 60-minute intervals
t = startTime:minutes(30):endTimeT;
% Convert datetime vector to datenum
tnum = datenum(t);
numTimePoints = length(t); % Corresponding to the length of your time vector t
numDistances = 500; % Example number of distance points or another parameter
% Create a matrix D with random data
D = rand(numTimePoints, numDistances);
% Create a logical mask for the desired time range
mask = (timeofday(t) >= timeofday(startTime)) & (timeofday(t) <= timeofday(endTime));
% Filter the data
D_filtered = D(mask, :);
tnum_filtered = tnum(mask);
% Plot the filtered data
figure;
imagesc(tnum, 1:size(D, 2), D', 'Interpolation', 'bilinear');
set(gca, 'YDir', 'normal');
colorbar;
% Customize x-ticks to show only start and end times
xticks([tnum_filtered(1), tnum_filtered(end)]);
xticklabels({datestr(tnum_filtered(1), 'HH:MM'), datestr(tnum_filtered(end), 'HH:MM')});
set(gca, 'XTickLabelRotation', 45);
% Optionally, add a dashed line to indicate a break
hold on;
yLimits = ylim;
plot([tnum_filtered(end), tnum_filtered(end)], yLimits, 'k--', 'LineWidth', 2);
hold off;
% Add labels and title as needed
xlabel('Time (HH:MM)');
ylabel('Distance');
title('Data From 6 AM to 12 midnight');
For more information regarding the "xticks" and "xticklabels" functions, refer to this documentation:
0 Commenti
Vedere anche
Categorie
Scopri di più su Bar Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!