Plotting negative values in boxplot

40 views (last 30 days)
Marguerite Lorenzo
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)
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 --
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...

Sign in to comment.

Answers (1)

Adam Danz
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]);
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;
yline(0,':','Color',[.6 .6 .6])
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 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.

Sign in to comment.



Community Treasure Hunt

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

Start Hunting!