MATLAB-在箱线图上表示不同类别数据的均值 [英] MATLAB - Plot means on boxplot for different categories of data

查看:1126
本文介绍了MATLAB-在箱线图上表示不同类别数据的均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a boxplot with six different categories plotted. These categories have been read in from different columns of an Excel spreadsheet. I would like to display the mean of each category on its associated box. After using the boxplot function, I have:

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

这将正确绘制第一类数据的平均值.但是,如果我继续执行代码并执行以下操作:

This plots the mean of the first category of data correctly. However, if I continue on with the code and do this:

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

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

it will plot the mean of data2 at the x-axis location of data1 instead of at the x-axis location of data2. Is there a way I can tell the program to plot each category's mean at specific x-axis locations that correspond to each category?

这是我代码的其余部分:

Here is the remainder of my code:

% 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

推荐答案

尝试一下:

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

在这里,我正在使用plot(x,y)语法.如果像您一样保留X,MATLAB会假设1.

Here I'm using the plot(x,y) syntax. If you leave X out, as you did, MATLAB assumes 1.

此外,您只需给一次hold on.

这篇关于MATLAB-在箱线图上表示不同类别数据的均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆