xregion yregion back/front control

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 comentarios

Bruno Luong
Bruno Luong el 22 de Mzo. de 2023
Editada: Bruno Luong el 22 de Mzo. de 2023
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.
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]);

Iniciar sesión para comentar.

 Respuesta aceptada

Adam Danz
Adam Danz el 22 de Mzo. de 2023
Editada: Adam Danz el 8 de Mayo de 2024
Update: Starting in R2024a, ConstantRegion has as Layer propert that you can set to Top or Bottom (see related answer).
@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 comentarios

Bruno Luong
Bruno Luong el 22 de Mzo. de 2023
Movida: Bruno Luong el 22 de Mzo. de 2023
Thanks @Adam Danz. All right. May be not the same issue in my original question, but
Can you explain the difference of color of the bottom left corners (outside the xregion area) between suplot 1 and 2 (top and bottom)?
It creates an illusion that the area of xregion with black axes background his hidden.
And if I set FaceAlpha=1, is not normal for both plots that the color changes between top-right (no intersection with histogram) and bottom-right (intersecting with histogram)? I expect to have red everywhere on top-right and bottom-roight; but it is dark red and dark green; I'm confuse.
I can control exactly the color-transparent I want with patch, but with xregion, it is still not clear to me.
Data = randn(1,1000);
xthrehold = 1;
figure
ax1 = subplot(211);
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', 1)
set(gca, 'XTick', [], 'YTick', [], 'ZTick', [], 'Color', [0 0 0]);
ax2 = subplot(212);
copyobj(get(ax1,'children'),ax2)
linkaxes([ax1 ax2]);
xlim(ax1, [0.9,1.1]);
Adam Danz
Adam Danz el 22 de Mzo. de 2023
Movida: Bruno Luong el 22 de Mzo. de 2023
Great observation @Bruno Luong.
I took a screenshot of your image and split the left and right sides into 2 different images below.
Can you explain the difference of color of the bottom left corners (outside the xregion area) between suplot 1 and 2 (top and bottom)?
The difference in the lower cyan color is because histograms are partially transparent. You can see through it so the black background makes the histogram area darker.
>It creates an illusion that the area of xregion with black axes background his hidden.
If you remove the left side, you'll that the illusion is gone. There is much greater contrast between the dark red and white (lower image) than between the dark red and black (upper image). The vertical border between the red and the white is much more visually stronger. As you mentioned, it's an illusion.
Currently, by default, ConstantRegion objects are behind other objects. So, from bottom to top in stacking order, you have the black axes, the red constant region, and the cyan histogram. There isn't a way to control the stacking order of the ConstantRegion but that's something we'll consider for a future release.
Bruno Luong
Bruno Luong el 22 de Mzo. de 2023
Movida: Bruno Luong el 22 de Mzo. de 2023
@Adam Danz "Currently, by default, ConstantRegion objects are behind other objects."
OK that confirms what I have observed, so I have two questions to follow up:
  • Currently what role play by the 'FaceAlpha' property of ConstantRegion object? Since there is no graphic object below it beside the axes itself? Does it change slighy the color depending on the color of the parent axes (or whatever parent container)?
  • Is there anyway for users to control the layer of the ConstantRegion and push it on top? If not would I suggest the enhancement request?
Currently because the ConstantRegion is behind my histogram I actually cannot control freely the color of the intersecting region between the histogram and the area of xregion.
I must use patch as workaround, which a little bit defeats usefulness of xregion. And probably yregion also suffers also the same lack of control.
Adam Danz
Adam Danz el 22 de Mzo. de 2023
Movida: Bruno Luong el 22 de Mzo. de 2023
> 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 el 22 de Mzo. de 2023
Movida: Bruno Luong el 22 de Mzo. de 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. :-)

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Labels and Annotations en Centro de ayuda y File Exchange.

Productos

Versión

R2023a

Preguntada:

el 22 de Mzo. de 2023

Editada:

el 8 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by