Plotting negative values in boxplot

43 ビュー (過去 30 日間)
Marguerite Lorenzo
Marguerite Lorenzo 2022 年 8 月 3 日
コメント済み: dpb 2022 年 8 月 4 日
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 件のコメント
Marguerite Lorenzo
Marguerite Lorenzo 2022 年 8 月 4 日
Hi, thank you for your response. I think the histrogram wouldn't be a bad way to look at the data, but am hoping to somehow visualize some of the statistics that the boxplot function offers (ie. mean, 25th and 75th percentile). The negative values, although small, have their importance - and from the boxplot in log scale (screenshot in my previous comment) 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....
dpb
dpb 2022 年 8 月 4 日
" 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...

サインインしてコメントする。

回答 (1 件)

Adam Danz
Adam Danz 2022 年 8 月 4 日
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);
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])
linkaxes([ax1,ax2],'x')
  2 件のコメント
Marguerite Lorenzo
Marguerite Lorenzo 2022 年 8 月 4 日
I like the idea of having a second zoomed in plot, thank you. Would there be a way to do something like this in log scale?
dpb
dpb 2022 年 8 月 4 日
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.

サインインしてコメントする。

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by