xregion yregion back/front control

26 vues (au cours des 30 derniers jours)
Bruno Luong
Bruno Luong le 22 Mar 2023
Déplacé(e) : Bruno Luong le 22 Mar 2023
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 commentaires
Bruno Luong
Bruno Luong le 22 Mar 2023
Modifié(e) : Bruno Luong le 22 Mar 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.
Bruno Luong
Bruno Luong le 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]);

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 22 Mar 2023
Déplacé(e) : Bruno Luong le 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 commentaires
Adam Danz
Adam Danz le 22 Mar 2023
Déplacé(e) : Bruno Luong le 22 Mar 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 le 22 Mar 2023
Déplacé(e) : Bruno Luong le 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. :-)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Labels and Annotations dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by