Using to the 'fill' function to plot a filled polygon that are confidence intervals
46 次查看(过去 30 天)
显示 更早的评论
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1076360/image.png)
But currently my figure looks like this in MATLAB
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1076365/image.jpeg)
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!!
0 个评论
采纳的回答
Cris LaPierre
2022-7-25
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
更多回答(2 个)
Steven Lord
2022-7-25
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
2022-7-25
编辑:Abderrahim. B
2022-7-25
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!