matlab - 如何更改给定箱线图图形的 'PlotStyle' 属性?

标签 matlab matlab-figure boxplot

给定 Matlab 箱线图的 .fig 文件(即基础数据不可用),是否可以更改 PlotStyle 属性(从“传统”到“紧凑”)?

最佳答案

这个问题有点棘手,因为与 Matlab 中的其他图形对象不同,boxplot 是一组线。因此,在您创建它时设置的所有属性在绘图后都无法访问(实际上不存在)。

处理该问题的一个方法是创建一个“虚拟”箱线图,然后将其更改为您的数据。因为 boxplot 没有 XDataYData 的简单属性,至少不像我们使用的那样,所以需要做一些工作。

这是一个简短的代码来证明:

% this is just to make a figure for example:
X = normrnd(10,1,100,1);
boxplot(X) % this is the 'Traditional' figure that you load
% you start here, after you load your figure:
bx = findobj('tag','boxplot');
% get the properties of the axes:
axlimx = bx.Parent.XLim;
axlimy = bx.Parent.YLim;
% get all needed properties for plotting compact box plot
txt = bx.Parent.XAxis.TickLabels;
med = get(findobj(bx,'tag','Median'),'YData'); % Median
out = get(findobj(bx,'tag','Outliers'),'YData'); % Outliers
box = get(findobj(bx,'tag','Box'),'YData'); % the Box
whis = cell2mat(get([findobj(bx,'tag','Lower Whisker')...
    findobj(bx,'tag','Upper Whisker')],'YData')); % Whisker
minmax = @(R) [min(R(:)) max(R(:))]; % helper function
close all
% Now we closed the original figure, and create a new one for manipulation:
boxplot(normrnd(10,1,100,1),'PlotStyle','Compact');
bxc = findobj('tag','boxplot');
% set the properties of the axes:
bxc.Parent.XLim = axlimx;
bxc.Parent.YLim = axlimy;
% set all properties of the compact box plot:
bxc.Children(1).String = txt;
set(bxc.Children(2),'YData',out) % Outliers
set(bxc.Children(3:4),'YData',med(1)) % MedianInner & MedianOuter
set(bxc.Children(5),'YData',minmax(box)) % the Box 
set(bxc.Children(6),'YData',minmax(whis)) % Whisker

将箱线图更改为“紧凑”样式的另一种方法是直接更改图形。在这种情况下,我们不会创建新的虚拟图形,而是处理加载的图形。

这是该方法的代码:

% this is just to make a figure for example:
X = normrnd(10,1,100,1);
boxplot(X) % this is the 'Traditional' figure that you load
% you start here, after you load your figure:
bx = findobj('tag','boxplot');
minmax = @(R) [min(R(:)) max(R(:))]; % helper function
% get the whisker limits:
whis = cell2mat(get([findobj(bx,'tag','Lower Whisker')...
    findobj(bx,'tag','Upper Whisker')],'YData')); % Whisker
set(findobj(bx,'tag','Upper Whisker'),{'YData','Color','LineStyle'},...
    {minmax(whis),'b','-'})
% set the median:
set(findobj(bx,'tag','Median'),{'XData','YData','LineStyle','Marker',...
    'Color','MarkerSize','MarkerFaceColor'},...
    {1,min(get(findobj(bx,'tag','Median'),'YData')),'none','o','b',6,'auto'}); 
% set the box:
set(findobj(bx,'tag','Box'),{'XData','YData','LineWidth'},...
    {[1 1],minmax(get(findobj(bx,'tag','Box'),'YData')),4});
im_med = copyobj(findobj(bx,'tag','Median'),bx);
im_med.Marker = '.';
% set the outliers:
out = get(findobj(bx,'tag','Outliers'),'YData'); % Outliers
set(findobj(bx,'tag','Outliers'),{'XData','LineStyle','Marker','MarkerEdgeColor',...
    'MarkerSize','MarkerFaceColor'},{0.9+0.2*rand(size(out)),'none','o','b',4,'none'});
% rotate x-axis labels:
bx.Parent.XAxis.TickLabelRotation = 90;
% delete all the rest:
delete(findobj(bx,'tag','Lower Whisker'))
delete(findobj(bx,'tag','Lower Adjacent Value'))
delete(findobj(bx,'tag','Upper Adjacent Value'))

关于matlab - 如何更改给定箱线图图形的 'PlotStyle' 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44525547/

相关文章:

matlab - 从变量读取的可变长度 MATLAB 参数

matlab - 参数均衡器的幅度响应

python - 删除 matplotlib boxplot 传单的边缘宽度

matlab - 什么是函数句柄,它有什么用?

matlab - 在 Matlab 中生成多元正态分布的随机数

matlab - 在纸上以精确尺寸打印 MATLAB 图

matlab - 在Matlab中使像素透明

matlab - 如何使matlab图例识别多个散点图?

r - 如何使用函数在 geom_boxplot 中放置文本

r - ggplot2:将 p 值添加到分组箱线图