xregion yregion back/front control

조회 수: 27 (최근 30일)
Bruno Luong
Bruno Luong 2023년 3월 22일
이동: Bruno Luong 2023년 3월 22일
I play with the new xregion command (R2023A)
The red region is hide by the histogram, not sure why it is not as document example and what should I do to bring it in front
Data = randn(1,1000);
figure
histogram(Data,20,'Normalization','pdf','FaceColor', 'c');
hold on
m = mean(Data);
s = std(Data);
x = linspace(m-3*s,m+3*s).';
g = 1/(s*sqrt(2*pi))*exp(-(x-m).^2/(2*s^2));
plot(x,g,'c', 'LineWidth', 2);
xregion(1, m+3*s,'FaceColor', [0.5 0 0], 'FaceAlpha', 1)
set(gca, 'XTick', [], 'YTick', [], 'ZTick', [], 'Color', 'k');
Note: I play with gca Children order, it doesn't seem to affect the front/back priority.
Ultimately what I want is this (here I use patch rather than xregion)
  댓글 수: 2
Bruno Luong
Bruno Luong 2023년 3월 22일
편집: Bruno Luong 2023년 3월 22일
It seems that the color of the region handles by xregion depend on axes background, barplot foreground and possibly other things.
The color change depending if I set axes color to 'k' or 'w', which is quite puzeling at least to me. The (obscure) behavior doesn't seem to be stated in the documentation page.
Not sure how to better control the thing.
Bruno Luong
Bruno Luong 2023년 3월 22일
For the record here is my work around using patch
Data = randn(1,1000);
xthrehold = 1;
figure
histogram(Data,20,'Normalization','pdf','FaceColor', 'c');
hold on
m = mean(Data);
s = std(Data);
x = linspace(m-3*s,m+3*s).';
g = 1/(s*sqrt(2*pi))*exp(-(x-m).^2/(2*s^2));
plot(x,g,'c', 'LineWidth', 2);
% xregion(xthrehold, m+3*s,'FaceColor', [0.5 0 0], 'FaceAlpha', 0.4) % this create region *behind* the histogram
% workaround using patch
x1 = xthrehold;
x2 = m+3*s;
[xrec,yrec] = meshgrid([x1 x2], ylim(gca));
K = convhull(xrec,yrec);
patch(xrec(K), yrec(K), [1 0 0], 'FaceAlpha', 0.4)
set(gca, 'XTick', [], 'YTick', [], 'ZTick', [], 'Color', [0 0 0]);

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2023년 3월 22일
이동: Bruno Luong 2023년 3월 22일
@Bruno Luong The color of the ConstantRegion does not depend on axis color or bar color. The difference in color you see is because
  1. Histograms are partially transparent by default
  2. ConstantRegions are also partially transparent by default
Take a look at the demo below.
Data = randn(1,1000);
m = mean(Data);
s = std(Data);
x = linspace(m-3*s,m+3*s).';
g = 1/(s*sqrt(2*pi))*exp(-(x-m).^2/(2*s^2));
figure
tiledlayout(3,2)
nexttile()
histogram(Data,20,'Normalization','pdf','FaceColor', 'c');
hold on
plot(x,g,'c', 'LineWidth', 2);
xregion(1, m+3*s,'FaceColor', [1 0 0])
title('Default')
nexttile()
histogram(Data,20,'Normalization','pdf','FaceColor', 'c');
hold on
plot(x,g,'c', 'LineWidth', 2);
xregion(1, m+3*s,'FaceColor', [1 0 0],'FaceAlpha',1)
title('ConstantRegion FaceAlpha=1')
nexttile()
histogram(Data,20,'Normalization','pdf','FaceColor', 'c');
hold on
plot(x,g,'c', 'LineWidth', 2);
xregion(1, m+3*s,'FaceColor', [1 0 0])
set(gca,'Color','k')
title('Default with black axes')
nexttile()
histogram(Data,20,'Normalization','pdf','FaceColor', 'c');
hold on
plot(x,g,'c', 'LineWidth', 2);
xregion(1, m+3*s,'FaceColor', [1 0 0],'FaceALpha',1)
set(gca,'Color','k')
title('ConstantRegion FaceAlpha=1')
nexttile()
histogram(Data,20,'Normalization','pdf','FaceColor', 'c','FaceAlpha',1);
hold on
plot(x,g,'c', 'LineWidth', 2);
xregion(1, m+3*s,'FaceColor', [1 0 0])
title('Histogram FaceAlpha=1')
nexttile()
histogram(Data,20,'Normalization','pdf','FaceColor', 'c','FaceAlpha',1);
hold on
plot(x,g,'c', 'LineWidth', 2);
xregion(1, m+3*s,'FaceColor', [1 0 0],'FaceAlpha',1)
title('Histogram & ConstantRgn FaceAlpha=1')
  댓글 수: 5
Adam Danz
Adam Danz 2023년 3월 22일
이동: Bruno Luong 2023년 3월 22일
> Currently what role play by the 'FaceAlpha' property of ConstantRegion object? Since there is no graphic object below it beside the axes itself?
The axis grid is also behind the ConstantRegion. Additionally, the faded color imposed by FaceAlpha has a less visually dominating effect. Unlike Patch, the intention of ConstantRegion is to highlight a vertical or horizontal band of data rather to produce a solid filled object, although you can certainly achieve that with ConstantRegion, too.
Data = randn(1,1000)*3;
m = mean(Data);
s = std(Data);
figure
tiledlayout(2,1)
nexttile
histogram(Data,20,'Normalization','pdf');
xregion(1, m+3*s)
grid on
nexttile
histogram(Data,20,'Normalization','pdf');
xregion(1, m+3*s,'FaceAlpha',1)
grid on
> Is there anyway for users to control the layer of the ConstantRegion and push it on top?
There isn't a way to control the stacking order of the ConstantRegion but that's something we can consider for a future release. Thanks for your interest, Bruno.
Bruno Luong
Bruno Luong 2023년 3월 22일
이동: Bruno Luong 2023년 3월 22일
Thanks @Adam Danz
As final word, try to control the color of the band of xregion in the dark mode (axes color is 'k') you would better understand my pain. :-)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Labels and Annotations에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by