Dashed confidence intervals and xlabel

1 view (last 30 days)
Today is my "plot day". I am trying to fit subplot with shaded confidence intervals (find the dataset attached). Besides, I have also a problem with xlabel. Let me write the code that is easier:
% data
data = xlsread('Q.xls')
lines = data(:,1:5)
lower_bound = data(:,6:10)
upper_bound = data(:,11:15)
k = 5
for j = 1:k
subplot(3, 2, j);
plot(lines(:,j), 'LineWidth',1,'Color', [0 0 0.5]);
hold on
fill(lower_bound(:,j), upper_bound(:,j), 'r', 'FaceAlpha', .1, 'EdgeColor', 'none');
xlabel('months');
yline(0, '-')
end
I also tried to use 'patch' but it didn't work either. What am I doing wrong? Besides, I would like 'xlabel' to be displayed only at the end of the first and second column; now, instead, it appears belowe every graph.
Can anyone help me fix this?

Accepted Answer

Star Strider
Star Strider on 31 May 2020
To plot as you want to plot, you need to create an ‘x’ vector to plot against. Since you are plotting against the row indices, create it to do just that. After that, use the ‘usual’ format for the patch calls. The xlabel call requires an if block (that may need to be revised if you add more plots).
The (Slightly-Revised) Code —
data = xlsread('Q.xls')
lines = data(:,1:5);
lower_bound = data(:,6:10);
upper_bound = data(:,11:15);
x = (1:size(data,1)).'; % Create ‘x’ To Plot Correctly
k = 5
for j = 1:k
subplot(3, 2, j);
plot(x, lines(:,j), 'LineWidth',1,'Color', [0 0 0.5]);
hold on
patch([x; flipud(x)], [lower_bound(:,j); flipud(upper_bound(:,j))], 'r', 'FaceAlpha', .1, 'EdgeColor', 'none');
if (j >= 4)
xlabel('months');
end
yline(0, '-')
end
The Plot —

More Answers (0)

Community Treasure Hunt

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

Start Hunting!