Patch or Fill between upper and lower bounds
Ältere Kommentare anzeigen
Hello Mathworks community,
I am looking to plot my upper and lower bounds with a shaded format (grey) instead of its current dotted line - no shading appearance(refer to attached image of an example). I am currently using the Gaussian Process Regression method and this would help significantly to highlight the error involved- please refer to my code provided below excluding the patch function (data is in N by 1):
Code:
tbl = readtable('m1.data.xlsx');
tbl.Properties.VariableNames = {'Temperature','Humidity','Mode1'};
gprMdl1 = fitrgp(tbl,'Mode1','KernelFunction','exponential',...
'FitMethod','Exact','PredictMethod','fic','Standardize',1);
[ytestpred,~,ytestci] = predict(gprMdl1,tbl,'Alpha',0.01);
figure();
plot(tbl.Mode1,'r','LineWidth',0.5);
hold on
plot(ytestpred,'b');
plot(ytestci(:,1),'k:');
plot(ytestci(:,2),'k:');
legend('Actual response','GPR predictions',...
'95% lower','95% upper','Location','Best');
hold off
Any help would be much appreciated.
Cheers, Cam
Antworten (1)
Star Strider
am 6 Okt. 2018
I am not certain what you want, or what you are referring to.
One option for the grey line:
x = 0:63;
y = sin(x/10);
figure
plot(x, y, '-k', 'LineWidth',2, 'Color',[1 1 1]*0.85) % Plot Grey Line
figure
patch([x(:); flipud(x(:))]', [y(:)+0.1; flipud(y(:)-0.1)]', 'k', 'FaceAlpha',0.2) % Plot ‘patch’ Object
figure
patch([x(:); flipud(x(:))]', [y(:)+0.1; flipud(y(:)-0.1)]', 'k', 'FaceAlpha',0.2, 'EdgeColor','none') % Plot ‘patch’ Object, No Edges
Experiment to get the result you want.
6 Kommentare
Campbell Dorotich
am 6 Okt. 2018
Campbell Dorotich
am 6 Okt. 2018
Star Strider
am 6 Okt. 2018
You need to introduce an independent variable in order to do the patch call. I created ‘t’ for that purpose.
Try this:
filename = 'Test Data.xlsx';
tbl = readtable(filename);
tbl.Properties.VariableNames = {'Temperature','Humidity','Mode1'};
t = (1:size(tbl(:,1),1))';
gprMdl1 = fitrgp(tbl,'Mode1','KernelFunction','exponential',...
'FitMethod','Exact','PredictMethod','fic','Standardize',1);
[ytestpred,~,ytestci] = predict(gprMdl1,tbl,'Alpha',0.01);
figure();
plot(tbl.Mode1,'r','LineWidth',0.5);
hold on
plot(ytestpred,'b');
% plot(ytestci(:,1),'k:');
% plot(ytestci(:,2),'k:');
patch([t; flipud(t)]', [ytestci(:,1); flipud(ytestci(:,2))]', 'k', 'FaceAlpha',0.2, 'EdgeColor','none')
hold off
legend('Actual response','GPR predictions',...
'±95% CI','Location','Best');
That code gives me this plot:

I simply commented-out your two previous plot calls for the confidence interval limits. Un-comment them to plot dotted lines at the limits. You will not need to change your legend call if you put the patch call before those two plot calls.
Experiment to get the result you want.
Campbell Dorotich
am 6 Okt. 2018
Star Strider
am 6 Okt. 2018
My pleasure.
If my Answer helped you solve your problem, please Accept it!
israt fatema
am 21 Feb. 2023
Bearbeitet: israt fatema
am 21 Feb. 2023
@Campbell Dorotich @Star Strider Hi, this is a very useful solution for my problem. I would like to know if you also done some evaluation for your prediction interval/GPR ? I have been looking for the answer how do i qualitatively evaluate Prediction interval accuracy using GPR? TIA
Kategorien
Mehr zu Descriptive Statistics 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!