MATLAB - 绘图表示不同类别数据的箱线图

时间:2018-03-06 19:29:07

标签: matlab

我有一个包含六个不同类别的箱线图。已从Excel电子表格的不同列中读取这些类别。我想在其关联的框中显示每个类别的平均值。使用boxplot函数后,我有:

hold on
plot(mean(data1), 'r+')

这正确地绘制了第一类数据的平均值。但是,如果我继续使用代码并执行此操作:

hold on
plot(mean(data1), 'r+')
hold on
plot(mean(data2), 'r+')

它将在data1的x轴位置绘制data2的平均值,而不是在data2的x轴位置。有没有办法告诉程序在每个类别对应的特定x轴位置绘制每个类别的平均值?

以下是我的代码的其余部分:

% create boxplots for altitudinal and areal extent for all categories

% read in altitudinal extent for all categories
% nontor
[~,~,raw] = xlsread('SupercellEnvironmentalVariables.xlsx',...
'nontornadic','C5:C48');
% find numbers
containsNumbers = cellfun(@isnumeric,raw);
% convert to string
raw(containsNumbers) = 
cellfun(@num2str,raw(containsNumbers),'UniformOutput',false);
NonAltExt = str2double(raw);

% tornadic
[~,~,raw] = xlsread('SupercellEnvironmentalVariables.xlsx',...
'tornadic','C5:C78');
% find numbers
containsNumbers = cellfun(@isnumeric,raw);
% convert to string
raw(containsNumbers) = 
cellfun(@num2str,raw(containsNumbers),'UniformOutput',false);
TorsAltExt = str2double(raw);

% all tornadoes
[~,~,raw] = xlsread('SupercellEnvironmentalVariables.xlsx',...
'tornadic','F5:F78');
% find numbers
containsNumbers = cellfun(@isnumeric,raw);
% convert to string
raw(containsNumbers) = 
cellfun(@num2str,raw(containsNumbers),'UniformOutput',false);
all_torsAltExt = str2double(raw);

% no tornadoes
[~,~,raw] = xlsread('SupercellEnvironmentalVariables.xlsx',...
'tornadic','O5:O78');
% find numbers
containsNumbers = cellfun(@isnumeric,raw);
% convert to string
raw(containsNumbers) = 
cellfun(@num2str,raw(containsNumbers),'UniformOutput',false);
notorAltExt = str2double(raw);

% weak tornadoes
[~,~,raw] = xlsread('SupercellEnvironmentalVariables.xlsx',...
'tornadic','I5:I78');
% find numbers
containsNumbers = cellfun(@isnumeric,raw);
% convert to string
raw(containsNumbers) = 
cellfun(@num2str,raw(containsNumbers),'UniformOutput',false);
weakAltExt = str2double(raw);

% significant tornadoes
[~,~,raw] = xlsread('SupercellEnvironmentalVariables.xlsx',...
'tornadic','L5:L78');
% find numbers
containsNumbers = cellfun(@isnumeric,raw);
% convert to string
raw(containsNumbers) = 
cellfun(@num2str,raw(containsNumbers),'UniformOutput',false);
sigAltExt = str2double(raw);


AltExt = [NonAltExt;TorsAltExt;all_torsAltExt;notorAltExt;...
weakAltExt;sigAltExt];
g = [ones(size(NonAltExt)); 2*ones(size(TorsAltExt)); 
3*ones(size(all_torsAltExt));...
4*ones(size(notorAltExt)); 5*ones(size(weakAltExt)); 
6*ones(size(sigAltExt))];

Figure1 = figure;
bh = boxplot(AltExt,g,'symbol','k*');
% patch fill each box to get a color fill with some transparency
h = findobj(gca,'Tag','Box');
for j=1:length(h)
    patch(get(h(j),'XData'),get(h(j),'YData'),'y','FaceAlpha',.5,'FaceColor',[1 1 1],...
     'EdgeColor',[0 0 0]);
end
hold on
plot(mean(NonAltExt),'r+')
title('Mean Z_D_R Col. Max. Altitude > 0\circC (km) for All Categories',...
'FontSize',12,'FontWeight','bold')
ylabel('Mean Z_D_R Col. Max. Altitude > 0\circC 
(km)','FontSize',10,'FontWeight','bold')
set(gca, 'Ticklength', [0 0])
set(gca,'xticklabel',[{'Nontornadic','Tornadic','All Tornado Sam. Vols.',...
'No Tornado Sam. Vols.','Weak Tornado Sam. Vols.',...
'Significant Tornado Sam. Vols.'}],'XTickLabelRotation',45)
set(bh(:,:),'linewidth',1.5);
ax = gca;
ax.YGrid = 'on';

% set color and increase thickness of median lines
median_lines = findobj(gca, 'type', 'line', 'Tag', 'Median');
set(median_lines, 'LineWidth',2.5, 'Color','r');
ax.Children = ax.Children([end 1:end-1]);   % allows median lines to be 
plotted on top of box fill

1 个答案:

答案 0 :(得分:0)

试试这个:

hold on
plot(1,mean(data1), 'r+')
plot(2,mean(data2), 'r+')

我在这里使用plot(x,y)语法。如果你把X放在外面,就像你一样,MATLAB假设为1。

此外,您只需要提供hold on一次。