How to skip missing time durations in a time-series heatmap (image based figure)

10 views (last 30 days)
I am working with timeseries data that I plot as a heatmap (image). The issue I am running into is when chunks of time are missing from the time series - it will repeat the last column of data until the time-series continues and then will continue the plotting. This becomes an issue when I also overlay a line plot that should correlate, as now they won't when this happens. Preferably, I just need the figure to leave a gap when there is no data for that time but have not figured it out yet! I thought about inserting NaN where data is missing, but that seems clunky. I am no matlab pro so I have had a hard time figuring it out! I have attached an example figure (that is for a random day where it looks good), data, and my code! Thank you so much! Code is below:
APSexample=[APSDAY10 APSDAY14(:,2:end) APSDAY15(:,2:end)];
APS=APSexample;
TotalConc_raw= APS(end,2:end); TotalConc=str2double(TotalConc_raw); %units of #/cm^3
APS_data_raw= APS(12:62,2:end); APS_data=str2double(APS_data_raw);
APS_diameter_raw=APS(12:62,1); APS_diameter=str2double(APS_diameter_raw);
C= readcell('SK25_APS_example.xlsx'); rawAPSweek2= C(12:62,2:end);dataAPSweek2=str2double(rawAPSweek2); writematrix(dataAPSweek2, 'SK24_APS_data_week2.xlsx');
D = datetime(C(8:9,1:end), 'InputFormat','MM/dd/yy', 'Format','yyyy/MM/dd HH:mm:ss','TimeZone','America/New_York') + duration(C(9,1:end), 'InputFormat','hh:mm:ss');
D = D(:);
DateTime=rmmissing(D); writematrix(DateTime,'SK25_APS_week3b_TIME.xlsx');
logdiam=log10(APS_diameter);
hold on
imagesc(DateTime,APS_diameter,APS_data)
yscale('log');
colorbar
cb = colorbar();
ylabel(cb,'dN/dlogDp','FontSize',11,'Rotation',270)
colormap turbo
xlabel 'DateTime'; ylabel ('Aerodynamic Diameter (µm)');
% ylim ([14.6 661.2]);
% yticks (linspace(14.6,661.2,5))
colororder ({'k'});
yyaxis right
plot(DateTime,TotalConc,LineStyle="-",Color="r")
ylabel ('Total Number Concentration (#/cm^3)', 'Rotation',270);
t1= DateTime(1,1); t2=DateTime(end,end);
xlim ([t1 t2]);
hold off
  3 Comments
Abigail Hobbs
Abigail Hobbs on 14 Apr 2025
oh wow that is on me! if you run the code, it will right the proper excel file for you with that name. ignore the one that i attached! I am sorry!
Abigail Hobbs
Abigail Hobbs on 14 Apr 2025
or wait, did I not include that line of code? I will just upload the correct excel file. sheesh, my bad!

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 14 Apr 2025
I don't think putting in nans where you don't want it to plot is "klunky" - we do it all the time. Go ahead and do it if that will solve the problem.
If you have missing data in what would otherwise be a nice solid grid of data coordinates, then you can use griddedInterpolant or scatteredInterpolant to fill in the missing parts (data values for the missing coordinate locations). See my attached demo of scatteredInterpolant.

Walter Roberson
Walter Roberson on 14 Apr 2025
Edited: Walter Roberson on 14 Apr 2025
imagesc(DateTime,APS_diameter,APS_data)
imagesc(x,y,C) specifies the image location. Use x and y to specify the locations of the corners corresponding to C(1,1) and C(m,n). To specify both corners, set x and y as two-element vectors. To specify the first corner and let imagesc determine the other, set x and y as scalar values. The image is stretched and oriented as applicable.
[...]
xPlacement along x-axis
[1 size(C,2)] (default) | two-element vector | scalar
Placement along the x-axis, specified in one of these forms:
  • Two-element vector — Use the first element as the location for the center of C(1,1) and the second element as the location for the center of C(m,n), where [m,n] = size(C). If C is a 3-D array, then m and n are the first two dimensions. Evenly distribute the centers of the remaining elements of C between those two points.The width of each pixel is determined by the expression:(x(2)-x(1))/(size(C,2)-1)If x(1) > x(2), then the image is flipped left-right.
  • Scalar — Center C(1,1) at this location and each following element one unit apart.
Note
  • If x has more than two elements, imagesc uses the first and last elements and ignores the other elements.
So imagesc() is going to ignore everything except the first and last entries in DateTime. It is not going to leave a gap where there are gaps in the DateTime vector.
There is no image-like function that will create a graphics gap for missing x data. The closest to that would be pcolor()... which will join together the edges of the missing areas (and has other undesireable properties.)
In order to get a gap for missing x data, you are going to need to detect missing x data yourself and split the image into pieces, drawing the different parts seperately. Or... you can fill your image data with NaN in the missing locations.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!