# Plotting negative values in boxplot

44 views (last 30 days)
Marguerite Lorenzo on 3 Aug 2022
Commented: dpb on 4 Aug 2022
I am trying to make a box plot of data in 6 different categories. Some of my data points are negative, and I am running into the problem that when I call the boxplot function it cuts off the y axis at 0 and I cannot get a good visual of the negative values. I am using MATLAB R2019a. Any insight on this would be appreciated;
flow_rates_w = NUM_w(1:183,24:29)
boxplot(flow_rates_w)
dpb on 4 Aug 2022
" it appears that for categories 2 and 6 the 25th percentiles fall in the negative range which and it would be nice to be able to see those values.."
Zoom the y axis --
ymn=min(flow_rates_w,[],'all');
yup=1E5;
ylim([ymn yup])
You could use tiledlayout and present the full-scale plot on one and the detail on a second -- having a builtin inset function would be handy for such things...

Adam Danz on 4 Aug 2022
You could add a second axes that zooms into the small, negative values.
flow_rates_w = NUM_w(1:183,:);
fig = figure();
tcl = tiledlayout(fig,4,1);
ax1 = nexttile(tcl,[3,1]);
boxplot(ax1,flow_rates_w);
Warning: boxplot might not be displayed properly in the tiled chart layout.
ax2 = nexttile(tcl);
boxplot(ax2,flow_rates_w); % or copyobj
Warning: boxplot might not be displayed properly in the tiled chart layout.
% zoom in to negative values
gmin = min(flow_rates_w,[],'all') * 1.1;
ylim(ax2,abs(gmin).*[-1,1])
yline(0,':','Color',[.6 .6 .6])
##### 2 CommentsShowHide 1 older comment
dpb on 4 Aug 2022
Only if you use the aforementioned "trick" of plotting abs(x) and then manually relabelling -- negative values simply don't have results in the real plane; you can't avoid that problem.
There is the FEX submission <sym_log> that shows how for ordinary data; doing the boxplot would take writing something similar for it to draw the various pieces for the negative data...one could possibly manage to extract the necessary pieces from the original although much of the content is hidden I think. I've not done any poking at the internals to see.

### Categories

Find more on Just for fun in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!