MatLab的:从采样数据创建3D柱状图 [英] MatLab: Create 3D Histogram from sampled data
问题描述
我已经在数组中取样区间[0,1]数据转换=零(101101)
我想绘制一个三维直方图。 转换
充满了类似的这些线的末尾提供的示例数据。
第一列指的是第一次观察到的变量 X
,第二列到第二个变量是
和第三列是归一化频率。即第一行:变量对(0,0)的观察归一化频率
为0.9459。归一化频率为的总和(0,Y)
因而 1
。
我试图让(在某种程度上)三维直方图具有以下code:
x_c =(转换(:,1)* 100)+1;
Y =(过渡(:,2)* 100)1;
Z =过渡(:,4);
%A =零(10,10);
A =零(MAX(x_c),最大值(Y));
对于i = 1:长度(x_c)
尝试
如果(Z(ⅰ)大于0)
A(INT32(x_c(I)),INT32(Y(I)))= ABS(日志(Z(I)));
其他
%处理异常有关日志(0)
A(INT32(x_c(I)),INT32(Y(I)))= 0;
结束
抓住
DISP('');
结束
结束
BAr 3的(A);
然而,由于它是在离散空间采样数据 A
输出看起来像下面的情节。这是有点误导,因为在情节'空白'(z值= 0的坐标在哪里我没有采样数据)。我倒想有采样数据被分配给其相应的地块,从而导致真正的3D柱状图。
顺便说一句,作为一个结果,我的'黑客'创造了 A
也的x,y和z规模是不正确的。三维直方图的轴(三个)应在区间[0,1]。
ANS =
0 0 0.9459
0 0.0500 0.0256
0 0.1000 0.0098
0 0.1100 0.0004
0 0.1500 0.0055
0 0.1600 0.0002
0 0.2000 0.0034
0 0.2100 0.0001
0 0.2500 0.0024
0 0.2600 0.0001
0 0.3000 0.0018
0 0.3200 0.0000
0 0.3700 0.0000
0 0.4000 0.0010
0 0.4200 0.0000
0 0.4500 0.0007
0 0.5000 0.0007
0 0.5300 0.0000
0 0.5500 0.0005
0 0.6000 0.0005
0 0.6300 0.0000
0 0.7000 0.0002
0 0.7400 0
0 0.7500 0.0003
0 0.7900 0.0000
0 0.8000 0.0002
0 0.8400 0.0000
0 0.8500 0.0002
0 0.8900 0.0000
0 0.9000 0.0002
0 0.9500 0.0001
0 1.0000 0.0001
0.0500 0 0.0235
0.0500 0.0500 0.0086
0.0500 0.1000 0.0045
。 。 。
。 。 。
。 。 。
。 。 。
。 。 。
0.9500 0.9000 0.0035
0.9500 0.9500 0.0066
0.9500 1.0000 0.0180
1.0000 0 0.0001
1.0000 0.0500 0.0001
1.0000 0.1000 0.0001
1.0000 0.1100 0.0000
1.0000 0.1500 0.0001
1.0000 0.1600 0.0000
1.0000 0.2000 0.0001
1.0000 0.2100 0.0000
1.0000 0.2500 0.0001
1.0000 0.2600 0.0000
1.0000 0.3000 0.0001
1.0000 0.3200 0.0000
1.0000 0.3700 0.0000
1.0000 0.4000 0.0002
1.0000 0.4200 0
1.0000 0.4500 0.0002
1.0000 0.5000 0.0003
1.0000 0.5300 0.0000
1.0000 0.5500 0.0004
1.0000 0.6000 0.0004
1.0000 0.6300 0.0000
1.0000 0.7000 0.0007
1.0000 0.7400 0.0000
1.0000 0.7500 0.0010
1.0000 0.7900 0.0000
1.0000 0.8000 0.0015
1.0000 0.8400 0.0001
1.0000 0.8500 0.0024
1.0000 0.8900 0.0002
1.0000 0.9000 0.0042
1.0000 0.9500 0.0111
1.0000 1.0000 0.3998
我找到了一个解决方案,通过工作的非总量数据。在数据集转换
特别每一行包含一个观察 X
和是对code>。我用下面的code生产标准化的3D直方图(和二维地图)作为folllows:
函数createHistogram(过渡)
唯一值=唯一的(过渡(:,1));
偏压=细胞(numel(唯一值),1);
对于i = 1:numel(唯一值)
开始=分钟(找到(过渡(:,1)==唯一值(ⅰ)));
STOP = MAX(发现(转换(:,1)==唯一值(I)));
偏见(I)= mat2cell(转换(START:停下来,2));
结束
combinedBiases = padcat(偏差{1},偏差{2},偏差{3},偏差{4},...
偏压{5},偏差{6},偏差{7},偏差{8},偏差{9},{偏压10},...
偏压{11},偏差{12},{偏压13},{偏压14},{偏压15},偏压{16},...
偏压{17},{偏压18},偏压{19});
仓= 0:0.1:1;
[F,X] = HIST(combinedBiases,箱);
%
%正常化
%
对于i = 1:numel(F(1,:))
对于j = 1:numel(F(:,I))
F(J,I)= F(J,I)/ numel(偏差{I});
结束
结束
bHandle = BAr 3的(X,F);
ylim([ - 0.04,1.04])
对于k = 1:长度(bHandle)
zdata =获取(bHandle(k)时,'ZData');
集(bHandle(k)时,'CData的',zdata,'FaceColor','的interp');
结束
颜色表('秋');
hcol =彩条();
轴('方');
CPOS = GET(hcol,'位置');
CPOS(4)= CPOS(4)/ 3; %减半厚度
CPOS(2)= 0.4; %将其向下移动以外的情节#
CPOS(1)= 0.82;
集(hcol,位置,CPOS);
xlabel('招生偏见');
ylabel('老化偏差');
zlabel('偏置变换概率');
标题(strcat的(,设备,偏见转换(概率密度函数'')'));
集(GCA,XTICK,0:2:20);
集(GCA,'XTickLabel',0:0.1:1);
打印( - dpng, - R600,strcat的('tau_PMF3D_enrollment-ageing-',设备));
图(2);
CPOS(1)= 0.84;
集(hcol,位置,CPOS);
打印( - dpng, - R600,strcat的('tau_PMF2D_enrollment-ageing-',设备));
结束
I have sampled data in the interval [0,1] in an Array transitions=zeros(101,101)
which I want to plot as a 3D-histogram. transitions
is filled with data similar to the example data provided at the end of this thread.
The first columns refers to the first observed variable X
, the second column to the second variable Y
and the third column is the normalized frequency. I.e. for the first row: the observed normalized frequency of the variable pair (0,0)
is 0.9459. The sum of the normalized frequencies for (0,Y)
thus is 1
.
I tried to make (sort of) a 3D histogram with the following code:
x_c = (transitions(:,1) * 100)+1;
y = (transitions(:,2) * 100)+1;
z = transitions(:,4);
%A = zeros(10,10);
A = zeros(max(x_c),max(y));
for i = 1:length(x_c)
try
if(z(i)>0)
A(int32(x_c(i)), int32(y(i))) = abs(log(z(i)));
else
% deal with exceptions regarding log(0)
A(int32(x_c(i)), int32(y(i))) = 0;
end
catch
disp('');
end
end
bar3(A);
However, since it is sampled data in a discrete space A
the output looks like the plot below. This is somehow misleading as there are 'gaps' in the plot (z-value = 0 for coordinates where I have no sampled data). I rather would like to have the sampled data being assigned to their corresponding plots, thus resulting in a 'real' 3d histogram.
By the way, as a result of my 'hack' of creating A
also the x-,y- and z-scale is not correct. The 3D histogram's axes (all three) should be in the interval of [0,1].
ans =
0 0 0.9459
0 0.0500 0.0256
0 0.1000 0.0098
0 0.1100 0.0004
0 0.1500 0.0055
0 0.1600 0.0002
0 0.2000 0.0034
0 0.2100 0.0001
0 0.2500 0.0024
0 0.2600 0.0001
0 0.3000 0.0018
0 0.3200 0.0000
0 0.3700 0.0000
0 0.4000 0.0010
0 0.4200 0.0000
0 0.4500 0.0007
0 0.5000 0.0007
0 0.5300 0.0000
0 0.5500 0.0005
0 0.6000 0.0005
0 0.6300 0.0000
0 0.7000 0.0002
0 0.7400 0
0 0.7500 0.0003
0 0.7900 0.0000
0 0.8000 0.0002
0 0.8400 0.0000
0 0.8500 0.0002
0 0.8900 0.0000
0 0.9000 0.0002
0 0.9500 0.0001
0 1.0000 0.0001
0.0500 0 0.0235
0.0500 0.0500 0.0086
0.0500 0.1000 0.0045
. . .
. . .
. . .
. . .
. . .
0.9500 0.9000 0.0035
0.9500 0.9500 0.0066
0.9500 1.0000 0.0180
1.0000 0 0.0001
1.0000 0.0500 0.0001
1.0000 0.1000 0.0001
1.0000 0.1100 0.0000
1.0000 0.1500 0.0001
1.0000 0.1600 0.0000
1.0000 0.2000 0.0001
1.0000 0.2100 0.0000
1.0000 0.2500 0.0001
1.0000 0.2600 0.0000
1.0000 0.3000 0.0001
1.0000 0.3200 0.0000
1.0000 0.3700 0.0000
1.0000 0.4000 0.0002
1.0000 0.4200 0
1.0000 0.4500 0.0002
1.0000 0.5000 0.0003
1.0000 0.5300 0.0000
1.0000 0.5500 0.0004
1.0000 0.6000 0.0004
1.0000 0.6300 0.0000
1.0000 0.7000 0.0007
1.0000 0.7400 0.0000
1.0000 0.7500 0.0010
1.0000 0.7900 0.0000
1.0000 0.8000 0.0015
1.0000 0.8400 0.0001
1.0000 0.8500 0.0024
1.0000 0.8900 0.0002
1.0000 0.9000 0.0042
1.0000 0.9500 0.0111
1.0000 1.0000 0.3998
I found a solution by working on the non-aggregated data. In particular each row of the data set transitions
contains one observation of X
and Y
. I used the code below to produce a normalized 3D histogram (and a 2D map) as folllows:
function createHistogram(transitions)
uniqueValues = unique(transitions(:,1));
biases = cell(numel(uniqueValues),1);
for i = 1:numel(uniqueValues)
start = min(find(transitions(:,1) == uniqueValues(i)));
stop = max(find(transitions(:,1) == uniqueValues(i)));
biases(i) = mat2cell(transitions(start:stop,2));
end
combinedBiases = padcat(biases{1},biases{2},biases{3},biases{4},...
biases{5},biases{6},biases{7},biases{8},biases{9},biases{10},...
biases{11},biases{12},biases{13},biases{14},biases{15},biases{16},...
biases{17},biases{18},biases{19});
bins = 0:0.1:1;
[f, x] = hist(combinedBiases, bins);
%
% normalize
%
for i = 1:numel(f(1,:))
for j = 1:numel(f(:,i))
f(j,i) = f(j,i)/numel(biases{i});
end
end
bHandle = bar3(x, f);
ylim([-0.04,1.04])
for k = 1:length(bHandle)
zdata = get(bHandle(k),'ZData');
set(bHandle(k),'CData',zdata, 'FaceColor','interp');
end
colormap('autumn');
hcol = colorbar();
axis('square');
cpos=get(hcol,'Position');
cpos(4)=cpos(4)/3; % Halve the thickness
cpos(2)=0.4; % Move it down outside the plot#
cpos(1)=0.82;
set(hcol, 'Position',cpos);
xlabel('Enrollment biases');
ylabel('Aging biases');
zlabel('Bias transition probability');
title(strcat('Probability mass function of bias transitions (', device,')'));
set(gca,'XTick',0:2:20);
set(gca,'XTickLabel',0:0.1:1);
print('-dpng','-r600',strcat('tau_PMF3D_enrollment-ageing-', device));
view(2);
cpos(1)=0.84;
set(hcol, 'Position',cpos);
print('-dpng','-r600',strcat('tau_PMF2D_enrollment-ageing-', device));
end
这篇关于MatLab的:从采样数据创建3D柱状图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!