MATLAB Answers

CIplot unable to fill on log-x axis

3 views (last 30 days)
Hi,
I'm trying to plot confidence intervals using CIplot (on the exchange) -- but I can't get it to fill between upper and lower when I switch the x-axis to log-scale. I include the function below. Any idea why it won't color between the lines when the x-axis is logscale?
function plot_handle = ciplot(lower,upper,x,colour,alpha);
% ciplot(lower,upper)
% ciplot(lower,upper,x)
% ciplot(lower,upper,x,colour)
% ciplot(lower,upper,x,colour,alpha)
%
% Plots a shaded region on a graph between specified lower and upper confidence intervals (L and U).
% l and u must be vectors of the same length.
% Uses the 'fill' function, not 'area'. Therefore multiple shaded plots
% can be overlayed without a problem. Make them transparent for total visibility.
% x data can be specified, otherwise plots against index values.
% colour can be specified (eg 'k'). Defaults to blue.
% Raymond Reynolds 24/11/06
%
% Add: 5th parameter, alpha (Default=50%)
% Pham Thai Binh 12/06/2017
if length(lower)~=length(upper)
error('lower and upper vectors must be same length')
end
if nargin<5
alpha=0.5;
end
if nargin<4
colour='k';
end
if nargin<3
x=1:length(lower);
end
% convert to row vectors so fliplr can work
if find(size(x)==(max(size(x))))<2
x=x'; end
if find(size(lower)==(max(size(lower))))<2
lower=lower'; end
if find(size(upper)==(max(size(upper))))<2
upper=upper'; end
plot_handle = fill([x fliplr(x)],[upper fliplr(lower)],colour,'FaceAlpha',alpha)

Accepted Answer

Star Strider
Star Strider on 6 May 2021
I am not certain what you are doing. However in many situations, it is easier to initially plot in a linear scale, then afterwards convert to log.
figure
plot( ... )
set(gca, 'XScale','log')
This is the only way to work with fill and patch with logarithmic axis scales.
Also, be certain that none of the data are NaN, since neither fill nor patch will work correctly if any of the data are NaN. If there are any, eliminate them by first locating them, then eliminating the rows with NaN values. One way to do that is:
data = [x(:) y(:)];
data = data(~any(isnan(data),2), :);
That should eliminate any NaN values that are present in the data set.
.
  2 Comments
Star Strider
Star Strider on 6 May 2021
As always, my pleasure!
I considered that possibility a few minutes ago, and was about to post this as an edit —
x = linspace(0,10).'; % Column Vectors
y = randn(size(x)) + 0.5*x; % Column Vectors
mdl = fitlm(x, y);
[yp,yci] = predict(mdl,x(:));
figure
plot(x, y, 'p')
hold on
plot(x, yp, '-r')
patch([x; flipud(x)], [yci(:,1); flipud(yci(:,2))], 'g', 'FaceAlpha',0.5)
hold off
Lv = x>0; % Eliminate Values Of ‘x’ Less Than Zero
figure
plot(x(Lv), y(Lv), 'p')
hold on
plot(x(Lv), yp(Lv), '-r')
patch([x(Lv); flipud(x(Lv))], [yci(Lv,1); flipud(yci(Lv,2))], 'g', 'FaceAlpha',0.5)
hold off
set(gca, 'XScale','log')
So I’ll post it here as a Comment!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by