如何更改给定boxplot数字的'PlotStyle'属性?

时间:2017-06-13 15:18:30

标签: matlab matlab-figure boxplot

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

1 个答案:

答案 0 :(得分:4)

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

处理这个问题的一个选择是创建一个“虚拟”' 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'))