boxchart and ylim auto

17 次查看(过去 30 天)
Guillaume Le Goc
Guillaume Le Goc 2020-4-8
Hello,
I'm struggling with a weird effect of the new (R2020a) boxchart function. The latter is essentially the same thing as boxplot, but much nicer.
I used to produce boxplots, and adding significance stars from pvalue using the sigstar function from Rob Campbell. The stars and bars were nicely displayed (not too high, not too low, not interfering with data...).
To get the good positions for the stars and bars, the strategy emploed is quite simple : make the axis tight, and zoom in the xaxis around the data. Since axis are set to tight, the y axis is automatically rescaled to match the min and max values of the displayed data. Extract the max value, add some offset, and everything is fine.
Now, when creating the boxplot with the boxchart function, and adding stars and bars with sigstar, the stars and bars are way too high.
After investigation, this is because after adjusting the x axis on a plot made with boxchart, the y axis is not automatically adjusted, even though the ax.YAxis.LimitsMode is set to 'auto', as if the internal algorithm used for adjusting axis still take into account the whole plot.
Any idea of why such behavior ? And any idea of a workaround ?
I would like to keep things simple. What I like with this method is that it is "graphic" exploiting the automatic features from Matlab, without the need to play around with the actual YData.
Cheers,
Guillaume
[Update] Here some code to reproduce the bug.
x = [1, 1, 2, 2, 3, 3];
y = [0.1, 0.15, 0.15, 0.2, 0.5, 0.8];
figure; boxplot(y, x);
xlim([0.9, 2.1]);
ylim auto
Produces :
While
figure; boxchart(x, y);
xlim([0.9, 2.1]);
ylim auto
Produces :
  2 个评论
Guillaume Le Goc
Guillaume Le Goc 2020-4-9
A temporary fix to achieve my needs is to get the YData from the zoomed x range to get the max value. It's kinda dirty.
x = [minrange, maxrange];
children = get(gca, 'Children');
prevmax = 0;
for idc = 1:numel(children)
S = children(idc);
if isa(S, 'matlab.graphics.primitive.Text')
continue;
end
% Most of graphics objects should have XData and YData. I hope...
ydatarange = S.YData(S.XData >= x(1) & S.XData <= x(2));
if ~isempty(ydatarange)
prevmax = max(prevmax, max(ydatarange));
end
end
Ymax = prevmax;
Scott MacKenzie
Scott MacKenzie 2021-5-6
编辑:Scott MacKenzie 2021-5-6
I don't think what you are observing is a bug. It has more to do with how you are using boxchart. For basic use, only one data variable (let's call it y) is provided as input to boxchart. Each box in the chart shows the data spread for one column of data in y
y = rand(10,3);
boxchart(y);
When you provide two data arguments as input, the first is treated as a grouping variable. In your first example, you provided y (equal to 0.1, 0.15, 0.15, 0.2, 0.5, 0.8) as the grouping variable. I don't think that's what you intended.

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Properties 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by