make patch white where there are NaNs
Ältere Kommentare anzeigen
Hi All,
I would like to use patch to make uncertainty bounds around timeseries data, but the data has NaNs. I can remove the NaNs from the patch datasets, but then I get nasty straight lines where it interpolates between the missing data. Since I am dealing with timeseries data the gaps in time with no data should remain. I thought of somehow making another white patch that goes only where there are NaNs but this would require dealing with every gap seperately, and finding their indices, which seems quite a pain given that there are many gaps. Any suggestons welcome!
Here's a simple example:
figure();
DT = datetime(2000,1,1):datetime(2000,12,31); DT=DT'; sizeDT = size(DT,1);
X_data = rand(sizeDT,1);
X_data(5:40)=NaN; X_data(100:120)=NaN; %make some NaN
idnans = isnan(X_data);
lb = X_data - (X_data.*0.3);
ub = X_data + (X_data.*0.3);
DTnn = DT; DTnn(idnans)=[]; lbnn = lb; lbnn(idnans)=[]; ubnn=ub; ubnn(idnans)=[];
patch([DTnn' fliplr(DTnn')],[lbnn' flipud(ubnn)'],'b','FaceAlpha',.2, 'EdgeColor','none'); hold on;
%modelled
plot(DT,X_data,'Color','b','LineStyle','-'); hold on;
legend('uncertainty','data');
Thanks!!
2 Kommentare
figure();
DT = datetime(2000,1,1):datetime(2000,12,31); DT=DT'; sizeDT = size(DT,1);
X_data = rand(sizeDT,1);
X_data(5:40)=NaN; X_data(100:120)=NaN; %make some NaN
idnans = isnan(X_data);
lb = X_data - (X_data.*0.3);
ub = X_data + (X_data.*0.3);
DTnn = DT; DTnn(idnans)=[]; lbnn = lb; lbnn(idnans)=[]; ubnn=ub; ubnn(idnans)=[];
patch([DTnn' fliplr(DTnn')],[lbnn' flipud(ubnn)'],'b','FaceAlpha',.2, 'EdgeColor','none'); hold on;
%modelled
plot(DT,X_data,'Color','b','LineStyle','-'); hold on;
legend('uncertainty','data');
It's not clear what you want the end result to be??? With a white background a white patch is/will be unseen, so what's the point?
As for dealing with the missing data sections, patch can create multiple polygons in a single call, where each is specificed by the corner coordinates in X,Y arrays; each column in X and Y defines a separate area. But, you will indeed have to locate the ends of the missing sections. How much trouble that may be will probably depend upon the sampling scheme; if it is a uniformly sampled time history, then any gap other than the sample time is missing and easy to find. Turning the data into a timetable would probably make things easier with retime.
Catriona Fyffe
am 5 Mär. 2025
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Polygons finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


