make patch white where there are NaNs
이전 댓글 표시
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
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
2025년 3월 5일
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


