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

10 ビュー (過去 30 日間)
Armando MAROZZI 2020 年 5 月 30 日
コメント済み: Adam Danz 2020 年 5 月 31 日
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):
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 件のコメント5 件の古いコメントを表示5 件の古いコメントを非表示
Adam Danz 2020 年 5 月 30 日
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 2020 年 5 月 30 日
@Adam Danz I attached an excel file with data, now the example should be reproducible and will hopefully enable you to sort it out

サインインしてコメントする。

採用された回答

Adam Danz 2020 年 5 月 30 日

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.
2 件のコメントなしを表示なしを非表示
Armando MAROZZI 2020 年 5 月 31 日
thanks a lot Adam! you were superb and sorry for not putting everything clearer since the beginning! Thanks again
Adam Danz 2020 年 5 月 31 日
That's quite alright. Glad I could help.

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by