Patch or Fill between upper and lower bounds

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

Answers (1)

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 Comments

Yeah I have attempted this type of code with no luck.
I will attach a sample of the data that I have to allow you to run the code and you can see what I am referring too. (essentially I would like the two dotted upper and lower bound lines) to be a grey fill.
Refer to image of current figure plotted (taking note of the dotted lines where I would like grey shading between the two of them)
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.
Perfect - Thank you for your assistance. This has answered my question to the full extent.
My pleasure.
If my Answer helped you solve your problem, please Accept it!
@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

Sign in to comment.

Asked:

on 6 Oct 2018

Edited:

on 21 Feb 2023

Community Treasure Hunt

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

Start Hunting!