xregion yregion back/front control

32 views (last 30 days)
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 Comments
Bruno Luong
Bruno Luong on 22 Mar 2023
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]);

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 22 Mar 2023
Moved: Bruno Luong on 22 Mar 2023
@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 Comments
Bruno Luong
Bruno Luong on 22 Mar 2023
Moved: Bruno Luong on 22 Mar 2023
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. :-)

Sign in to comment.

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by