Is there a way to get shaded error regions on a scatter plot?

Hi folks,
I have a scatter plot with known errors for each datapoint. Is there a way to create a shaded region around the scatter plot? Below is my graph, the red and cyan regions are the errors and the blue is the data. Is there a way to shade them and make them a continuous line, like the graph below?
I've attached the table below, along with my code.
Thanks
figure;
hold on;
scatter(myData.Anisotropic_1, myData.AnisoMin);
scatter(myData.Anisotropic_1, myData.AnisoMax);
scatter(myData.Anisotropic_1, myData.("MPC aniso"), 'b');
alpha(0.1);
title('Axiovision versus MPC, with reproducibility (r) in shaded region',"FontWeight","bold");
xlabel('Axiovision', "FontWeight","bold");
ylabel('MPC', "FontWeight","bold");
hold off;

2 Comments

If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:
@Image Analyst thanks, I've updated the post with my code and the relevant data!

Sign in to comment.

 Accepted Answer

It would help to have the data.
Try something like this —
x = linspace(0, 2*pi, 50);
y = sin(x) + 0.5;
err = rand(size(x))*0.5 + 0.5;
figure
scatter(x, y, 'filled')
hold on
patch([x, flip(x)], [y-0.25 flip(y+err)], 'b', 'FaceAlpha',0.25, 'EdgeColor','r', 'LineStyle','-.', 'MarkerSize',20)
hold off
grid
Experiment to get the desired result.
.

4 Comments

@Star Strider Hi, unfortunately, this doesn't seem to work for me. May I ask what I might be doing wrong please?
xfill = [myData.Anisotropic_1 fliplr(myData.Anisotropic_1)];
obs_anisofill = [myData.AnisoMax fliplr(myData.AnisoMin)];
figure;
hold on;
% fill(xfill, obs_anisofill);
% plot(myData.Anisotropic_1, myData.AnisoMin);
% plot(myData.Anisotropic_1, myData.AnisoMax);
scatter(myData.Anisotropic_1, myData.("MPC aniso"), 'b');
h = lsline;
h.Color = 'b';
[p1, s1] = polyfit(get(h,'xdata'),get(h,'ydata'),1);
f1 = polyval(p1, myData.Anisotropic_1);
rs1 = my_Rsquared_coeff(myData.("MPC aniso"), f1);
caption1 = sprintf('y = %0.2fx + %0.2f, R^2 = %.2f', p1(1), p1(2), rs1);
text(min(myData.Anisotropic_1)+20, max(myData.("MPC aniso"))-40, caption1, 'FontSize', 10, 'Color', 'b', 'FontWeight', 'bold');
% plot(polyval(FitAniso, myData.Anisotropic_1));
patch(xfill, obs_anisofill, 'b', 'FaceAlpha',0.1, 'EdgeColor','r', 'LineStyle','-.', 'MarkerSize',20)
title('Axiovision versus MPC, with reproducibility (r) in shaded region',"FontWeight","bold");
xlabel('Axiovision', "FontWeight","bold");
ylabel('MPC', "FontWeight","bold");
hold off;
The vectors used for the patch call need to be sorted, since they are not originally. This sorts ‘myData.Anisotropic_1’ and then uses that index to reference the other vectors. Also, my code used row vectors, your uses column vectors, so that required a minor tweak. With those changes, it works.
Try this —
LD = load(websave('myData','https://www.mathworks.com/matlabcentral/answers/uploaded_files/1182658/myData.mat'));
myData = LD.myData
myData = 73×23 table
Sample Name Resin All Coke Anisotropic Dark Resin% Coke% Anisotropic_1 Dark_1 MPC aniso MPC dark Repeatability AnisoMax AnisoMin DarkMax DarkMin CRI Reflectance Var19 Anisotropic_2 Dark_2 MPC aniso_1 MPC dark_1 __________________________ __________ __________ ___________ __________ ______ ______ _____________ ______ _________ ________ _____________ ________ ________ _______ _______ ___ ___________ _____ _____________ ______ ___________ __________ {'AFCO 16-OCT-18' } 2.4344e+08 1.1445e+08 9.0583e+07 2.3872e+07 68.02 31.98 79.143 20.857 78.13 21.87 5.2079 84.351 73.935 26.065 15.649 NaN NaN NaN NaN NaN NaN NaN {'Morfa float 27-01-21' } 2.3271e+08 1.2519e+08 6.1369e+07 6.3817e+07 65.022 34.978 49.023 50.977 49.62 50.38 6.2992 55.322 42.723 57.277 44.678 NaN NaN NaN NaN NaN NaN NaN {'Morfa sink 23-02-21' } 2.4379e+08 1.1411e+08 7.8821e+07 3.5287e+07 68.117 31.883 69.076 30.924 67.97 32.03 5.8785 74.955 63.197 36.803 25.045 NaN NaN NaN NaN NaN NaN NaN {'AFCO 3-APR-19' } 2.3063e+08 1.2727e+08 7.7228e+07 5.0041e+07 64.44 35.56 60.681 39.319 62.28 37.72 6.1065 66.787 54.574 45.426 33.213 NaN NaN NaN NaN NaN NaN NaN {'Morfa Float 24-02-21' } 2.2987e+08 1.2803e+08 8.4423e+07 4.3603e+07 64.228 35.772 65.942 34.058 66.74 33.26 5.9359 71.878 60.006 39.994 28.122 NaN NaN NaN NaN NaN NaN NaN {'Morfa Sink 30-01-21' } 2.2898e+08 1.2892e+08 7.5212e+07 5.3708e+07 63.979 36.021 58.34 41.66 57.57 42.43 6.2268 64.567 52.113 47.887 35.433 NaN NaN NaN NaN NaN NaN NaN {'Morfa Week 39 28-09-20'} 2.9123e+08 6.6666e+07 5.2124e+07 1.4542e+07 81.373 18.627 78.187 21.813 77.29 22.71 5.2784 83.465 72.908 27.092 16.535 NaN NaN NaN NaN NaN NaN NaN {'MORFA FLOAT 09-NOV-21' } 2.9031e+08 6.7587e+07 4.511e+07 2.2476e+07 81.116 18.884 66.744 33.256 67.54 32.46 5.8991 72.643 60.845 39.155 27.357 NaN NaN NaN NaN NaN NaN NaN {'MORFA FLOAT 31-OCT-21' } 2.7808e+08 7.9817e+07 4.5361e+07 3.4457e+07 77.698 22.302 56.831 43.169 55.63 44.37 6.2593 63.09 50.571 49.429 36.91 NaN NaN NaN NaN NaN NaN NaN {'IJmuiden 50CRI' } 2.2358e+08 1.3432e+08 6.8614e+07 6.5704e+07 62.47 37.53 51.083 48.917 50.68 49.32 6.2988 57.382 44.785 55.215 42.618 NaN NaN NaN NaN NaN NaN NaN {'MORFA SINK 24-FEB-21' } 2.2577e+08 1.3213e+08 8.7576e+07 4.4556e+07 63.081 36.919 66.279 33.721 67.28 32.72 5.9113 72.19 60.368 39.632 27.81 NaN NaN NaN NaN NaN NaN NaN {'MORFA FLOAT 25-OCT-21' } 2.4612e+08 1.1177e+08 6.2124e+07 4.965e+07 68.769 31.231 55.58 44.42 56.38 43.62 6.2479 61.828 49.332 50.668 38.172 NaN NaN NaN NaN NaN NaN NaN {'MORFA FLOAT 15-NOV-21' } 2.3216e+08 1.2574e+08 6.6332e+07 5.9408e+07 64.867 35.133 52.753 47.247 51.65 48.35 6.296 59.049 46.457 53.543 40.951 NaN NaN NaN NaN NaN NaN NaN {'MORFA GROUND 2' } 2.6764e+08 9.0259e+07 4.466e+07 4.5599e+07 74.781 25.219 49.48 50.52 47.38 52.62 6.2908 55.77 43.189 56.811 44.23 NaN NaN NaN NaN NaN NaN NaN {'MORFA GROUND 3' } 2.6259e+08 9.5305e+07 4.5938e+07 4.9368e+07 73.371 26.629 48.201 51.799 50.1 49.9 6.2994 54.5 41.901 58.099 45.5 NaN NaN NaN NaN NaN NaN NaN {'MORFA GROUND 4' } 2.5613e+08 1.0177e+08 6.4902e+07 3.6868e+07 71.564 28.436 63.773 36.227 61.37 38.63 6.1344 69.908 57.639 42.361 30.092 NaN NaN NaN NaN NaN NaN NaN
[patchx,pidx] = sort(myData.Anisotropic_1); % Sort, Return Index
patchy1 = myData.AnisoMax(pidx); % Use 'pidx'
patchy2 = myData.AnisoMin(pidx); % Use 'pidx'
xfill = [patchx; flipud(patchx)];
obs_anisofill = [patchy1; flipud(patchy2)];
figure;
hold on;
% % fill(xfill, obs_anisofill);
% % plot(myData.Anisotropic_1, myData.AnisoMin);
% % plot(myData.Anisotropic_1, myData.AnisoMax);
scatter(myData.Anisotropic_1, myData.("MPC aniso"), 'b');
h = lsline;
h.Color = 'b';
[p1, s1] = polyfit(get(h,'xdata'),get(h,'ydata'),1);
f1 = polyval(p1, myData.Anisotropic_1);
% rs1 = my_Rsquared_coeff(myData.("MPC aniso"), f1); % 'my_Rsquared_coeff' Doesn't Exist
% caption1 = sprintf('y = %0.2fx + %0.2f, R^2 = %.2f', p1(1), p1(2), rs1);
% text(min(myData.Anisotropic_1)+20, max(myData.("MPC aniso"))-40, caption1, 'FontSize', 10, 'Color', 'b', 'FontWeight', 'bold');
% plot(polyval(FitAniso, myData.Anisotropic_1));
patch(xfill, obs_anisofill, 'b', 'FaceAlpha',0.1, 'EdgeColor','r', 'LineStyle','-.', 'MarkerSize',20)
title('Axiovision versus MPC, with reproducibility (r) in shaded region',"FontWeight","bold");
xlabel('Axiovision', "FontWeight","bold");
ylabel('MPC', "FontWeight","bold");
hold off;
Some information (‘rs1’) is missing, so I commented it out and the lines using it out so the rest of the code will run.
Make appropriate changes to get the desired result.
.
The alternative to sorting is to use boundary() with an alpha value to find the outline of the containing shape.
I’ve done that in the past, especailly when the outline to be filled would not yield to sorting and remained essentially impossible to define. Here, sorting worked.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2022b

Community Treasure Hunt

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

Start Hunting!