Plotting negative values in boxplot
40 views (last 30 days)
Show older comments
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)

10 Comments
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])
adjust as desired.
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...
Answers (1)
Adam Danz
on 4 Aug 2022
You could add a second axes that zooms into the small, negative values.
[NUM_w,TXT_w,RAW_w] = xlsread("data_boxplot");
flow_rates_w = NUM_w(1:183,:);
fig = figure();
tcl = tiledlayout(fig,4,1);
ax1 = nexttile(tcl,[3,1]);
boxplot(ax1,flow_rates_w);
ax2 = nexttile(tcl);
boxplot(ax2,flow_rates_w); % or copyobj
% 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])
linkaxes([ax1,ax2],'x')
2 Comments
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!