MATLAB Answers

boxchart and ylim auto

21 views (last 30 days)
Guillaume Le Goc
Guillaume Le Goc on 8 Apr 2020
Edited: Scott MacKenzie on 6 May 2021
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 Comments
Scott MacKenzie
Scott MacKenzie on 6 May 2021
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.

Sign in to comment.

Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!