Boxplot and histogram in one plot
65次观看(最近30天)
Show older comments
嘿,
我认为这个问题以前曾被问到过:
但我找不到任何合适的解决方案;
so is there a way to achieve something like this:
I'm looking forward to hear your suggestions :)
1条评论
Sambit Supriya Dash
2021年5月6日
是的,可以在一个图中获取Boxplot和直方图,
Example,
x = [1 2 3; 4 5 6; 7 8 9];
y= [5 4 3; 5 7 9; 1 9 3];
figure(1)
历史(x)
抓住on
箱形图(y)
抓住离开
Hope, it helps.
Accepted Answer
亚当·丹兹(Adam Danz)
on 7 May 2021
Edited:亚当·丹兹(Adam Danz)
on 7 May 2021
This method uses patch objects to display histograms next to each boxplot. It's based on
another answer
这显示了垂直散点图旁边的概率密度分布。
您只需要替换X和Y数据,即可复制代码的其余部分。
输入。
X
: 1xn vector defining the x coordinate of n boxplots.
y
: mxn matrix of raw data for n boxplots
rng('default')%可再现性
x = 0:5:30;
y =(randn(300,numel(x)) + linspace(.5,5,numel(x)))。* linspace(.5,2,numel(x));
设置间距
binWidth = 0.4;%直方图箱宽度
hgapGrp = .05;Boxplot/直方图对之间的水平差距%(归一化)
HGAP = 0.2;% horizontal gap between boxplot and hist (normalized)
Compute histogram counts & edges
hcounts
is an nx2 cell array containing the {counts, edges} for each distribution.
MaxCount
is the maximum bin count across all distributionts, used to normalize patch heights
hcounts =单元格(size(y,2),2);
为了i = 1:size(y,2)
[hcounts{i,1}, hcounts{i,2}] = histcounts(y(:,i),'BinWidth',,,,binWidth);
结尾
MaxCount= max([hcounts{:,1}]);
情节框图
无花果=图();
ax = axes(fig);
抓住(ax,'上')
Xinterval = emane(diff(stort(x)));% x-interval (best if x is at a fixed interval)
normwidth = (1-hgapGrp-hgap)/2;
箱形图Width = xInterval*normwidth;
Boxplot(Ax,Y,'Positions',,,,X,,,,'Widths',BoxPlotWidth,“离群值”,,,,3,'Labels',撰写('%d',,,,X))
Add vertical histograms (patches)
HistX0 = X + BoxPlotWidth/2 + HGAP;% histogram base
maxheight = xinterval*normWidth;%最大直方图
patchHandles = gobjects(1,size(y,2));
为了i = 1:size(y,2)
% Normalize heights
高度= hcounts {i,1}/maxcount*maxheight;
%计算X和Y坐标
Xm = [zeros(1,numel(height)); repelem(height,2,1); zeros(2,numel(height))] + histX0(i);
yidx = [0 0 1 1 0]' + (1:numel(height));
ym = hcounts {i,2}(yidx);
% Plot patches
patchHandles(i)= patch(xm(:),ym(:),[0 .75 1],'FaceAlpha',.4);
结尾
Xlim([-2.5, 32.5])
Method 2 with color control
With just a few changes to the code above, you can use
BoxPlotGroup()
从文件交换到设置框图的颜色和直方图,类似于您的问题中的示例。
%%输入
rng('default')%可再现性
x = 0:5:30;
y =(randn(300,numel(x)) + linspace(.5,5,numel(x)))。* linspace(.5,2,numel(x));
%% Set Spacing
binWidth = 0.4;%直方图箱宽度
hgapGrp = .15;Boxplot/直方图对之间的水平差距%(归一化)
hgap = 0.06;% horizontal gap between boxplot and hist (normalized)
%% Compute histogram counts & edges
hcounts =单元格(size(y,2),2);
为了i = 1:size(y,2)
[hcounts{i,1}, hcounts{i,2}] = histcounts(y(:,i),'BinWidth',,,,binWidth);
结尾
MaxCount= max([hcounts{:,1}]);
%% Plot boxplotsGroup()
无花果=图();
ax = axes(fig);
抓住(ax,'上')
%转换Y(MXN矩阵)为MX1矢量的1xN单元格数组,BoxPlotWidths要求
yc = mat2cell(y,size(y,1),ones(1,size(y,2)));
XInterval = 1;%x间隙总是1个带有框图组的1
normwidth = (1-hgapGrp-hgap)/2;
箱形图Width = xInterval*normwidth;
% Define colors for each boxplot
颜色=线条(size(y,2));
%情节彩色盒子图
BPH = BoxPlotGroup(AX,YC,'Widths',BoxPlotWidth,“离群值”,,,,3,'PrimaryLabels',撰写('%d',,,,X),'Colors',颜色);
set(findobj(bph.boxplotgroup,'-property',,,,'LineWidth'),'LineWidth',,,,1)% increase line widths
%% Add vertical histograms (patches) with matching colors
xCoorcorion = 1:size(y,2);%x位置总是1:n,带有框图组
HistX0 = XCoorcorion + BoxPlotWidth/2 + HGAP;% histogram base
maxheight = xinterval*normWidth;%最大直方图
patchHandles = gobjects(1,size(y,2));
为了i = 1:size(y,2)
% Normalize heights
高度= hcounts {i,1}/maxcount*maxheight;
%计算X和Y坐标
Xm = [zeros(1,numel(height)); repelem(height,2,1); zeros(2,numel(height))] + histX0(i);
yidx = [0 0 1 1 0]' + (1:numel(height));
ym = hcounts {i,2}(yidx);
% Plot patches
patchHandles(i)= patch(xm(:),ym(:),颜色(i,:),,,,'edgeColor',颜色(i,:),,'LineWidth',,,,1,,,,'FaceAlpha',,,,。45);
结尾
更多答案(1)
斯科特·麦肯齐(Scott Mackenzie)
2021年5月6日
Edited:斯科特·麦肯齐(Scott Mackenzie)
on 7 May 2021
This solution uses
Tiledlayout
and
boxchart
,,,,but you can adjust to use
subplot
and
箱形图
if you are running an older version of MATLAB:
n = 7;
y= randn(100,n);
TileDlayout(1,2*n);
为了i = 1:n
nexttile;
boxchart(y(:,i));
set(gca,'visible',,,,'off',,,,'ylim',[-4 4]);
nexttile;
直方图(y(:,i),20,'orientation',,,,'horizontal');
set(gca,'visible',,,,'off',,,,'ylim',[-4 4]);
结尾
f = gcf;
f.Color ='W';
f.Units =“归一化”;
f.Position = [.1 .2 .8 .4];