Clear Filters
Clear Filters

How to plot two different styles of confidence intervals in looped subplots?

2 views (last 30 days)
I have a plot with 6 variables and 5 subplot. In one case, the third variable, I want it to display two lines in one graph. So far, it is fine. However, I also need confidence intervals to differ in shap for this subplot, the alternative ones should be dashaded while the others shaded. I don't know how to do it.
This is an example (data on attached excel file):
data = xlsread('prova.xls') % Data
lines = data(:, 1:5)
line2 = data(:, 16)
ci = data(:, 6:15)
ci2 = data(:,17:18)
k = 5 % plot
for j = 1:k
subplot(3, 2, j);
plot(lines(:,j), 'LineWidth',1,'Color', [0 0 0.5]);
hold on
if j == 3
plot(line2, 'LineWidth',1,'Color', [0 0 0.5], 'LineStyle','--')
end
hold on
plot(ci,'LineWidth',1,'Color', [0 0 0.5],'LineStyle','--');
hold on
% plot(ci2,'LineWidth',1,'Color', [0 0 0.5],'LineStyle','--'); % don't know how to plot it
% I tried: if j == 3
% plot(ci2, 'LineWidth',1,'Color', [0 0 0.5], 'LineStyle','--mo')
% end but it didn't work
yline(0, '-')
end
I would like the code to be modified to embed: a) the confidence intervals of the second dataset only in subplot 3; b) a different confidence interval's style (shaded vs dashed). I am stuck and don't know how to do it unfortunately.
Is there anyone who can help me?
Thanks!
  7 Comments
Adam Danz
Adam Danz on 30 May 2020
The updated demo is still confusing.
For example, instead of plotting ci as lines, I plotted them as circular markers and this is the first subplot. It's not clear how those markers are supposed to represent confidence intervals.
Armando MAROZZI
Armando MAROZZI on 30 May 2020
@Adam Danz I attached an excel file with data, now the example should be reproducible and will hopefully enable you to sort it out

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 30 May 2020
Edited: Adam Danz on 31 May 2020
The line below from your code is plotting 30 lines for each axis.
plot(ci,'LineWidth',1,'Color', [0 0 0.5],'LineStyle','--');
because ci is a 30x10 matrix and a line is being plotted for each column.
Perhaps ci(:,j) is the confidence interval range whereby the use of errorbar() will show confidence intervals. I think this is what you're pursuing.
errorbar(lines(:,j), ci(:,j)) % you can get rid of the previous plot() line
hold on % only needs to be called once per axes
patch([1:size(ci2,1),size(ci2,1):-1:1]', ci2(:), 'r', 'FaceAlpha', .1, 'EdgeColor', 'none')
errorbar(y,err) plots symetric error bars for each element of y.
patch() produces the patch object and you can set its color and transparency.
See the pages in the documentation for each funciton to learn how to change other properties.
An example of these lines applied to one of your subplots is below. Note the strange reversal of the patch CI toward the end of the plot. That's in your data so you'll have to decide if this is exected or not.
Note, this assumes ci2 contains the positions of the lower/upper CIs. If ci2 contains the *offsets*, then you'll need to add the ci2 to your y-values when calling the patch() function.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!