Using to the 'fill' function to plot a filled polygon that are confidence intervals

17 views (last 30 days)
Hello! I need some help plotting some confidence intervals for a figure. Where my x-values (counting up, then counting down) are being plotted on the x-axis and y-values should be the upper 95% C.I. (counting up) and the lower 95% C.I. (counting down). For reference, I want to make a figure that looks like this
But currently my figure looks like this in MATLAB
This is my current, simplified code
x = (data(:1)); %x-axis data, 1 x 16 double
y = (data(:2)); %original data, 1 x 16 double
CIlow = (data(:3)); %lower cofidence range, 1 x 16 double
CIhigh = (data(:4)); %upper confidence range, 1 x 16 double
figure
plot(log10(x),log10(y),'-b','LineWidth',2,'Color','g');
hold on
fill([(log10(x)),fliplr(log10(x))], [log10(CIlow),fliplr(log10(CIhigh))], 'b', 'EdgeColor','none', 'FaceAlpha',0.25)
Basically I want to use the 'fill' function to plot a filled polygon. The fill function seems like it's not being added onto the graph, even while I play around with hold on and hold off. Any ideas? Thanks in advance!!

Accepted Answer

Cris LaPierre
Cris LaPierre on 25 Jul 2022
Your code looks fine. Check what your values of CIlow and CIhigh are.
x = logspace(0,140,16); %x-axis data, 1 x 16 double
y = rand(size(x)); %original data, 1 x 16 double
CIlow = y-0.1; %lower cofidence range, 1 x 16 double
CIhigh = y+0.1; %upper confidence range, 1 x 16 double
figure
plot(log10(x),log10(y),'-b','LineWidth',2,'Color','g');
hold on
fill([(log10(x)),fliplr(log10(x))], [log10(CIlow),fliplr(log10(CIhigh))], 'b', 'EdgeColor','none', 'FaceAlpha',0.25)
hold off

More Answers (2)

Steven Lord
Steven Lord on 25 Jul 2022
Let's draw the region "around" a simpler line.
x = 1:10;
y = x;
plot(x, y, 'k', 'DisplayName', 'Original Line');
legend show
figure
plot([x, fliplr(x)], [y, fliplr(y)], 'g', 'DisplayName', 'Fill Region')
legend show
How "thick" is the fill region line compared to the original line?
figure
plot([x, fliplr(x)], [y-0.1, fliplr(y)+0.1], 'r', 'DisplayName', 'Padded')
legend show
How "thick" is the padded region compared to the original line?
Note that if I'd just plotted the confidence interval proxy of 0.1 units, without adding in the y values:
figure
plot(x, y, 'k', 'DisplayName', 'Original')
hold on
plot(x, repmat(0.1, size(x)), 'g', 'DisplayName', 'padding')
legend show
The green line is not anywhere near the original line. Your confidence intervals are likely being plotted but they're outside the region displayed based on your Y axis limits.

Abderrahim. B
Abderrahim. B on 25 Jul 2022
Edited: Abderrahim. B on 25 Jul 2022
Hi!
patch function is an option, maybe there are other ways to do this.
Perhaps this:
x = (data(:1)); %x-axis data, 1 x 16 double
y = (data(:2)); %original data, 1 x 16 double
CIlow = (data(:3)); %lower cofidence range, 1 x 16 double
CIhigh = (data(:4)); %upper confidence range, 1 x 16 double
figure
patch([log10(x) fliplr(log10(x))] ,[log10(CIlow) fliplr(log10(CIhigh))], 'g')
hold on
plot(log10(x),log10(y), 'r--');
legend confidence data
hold off
Copy, past and run this on your data and let me know.
Hope this helps

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!