3D直方图和条件着色

时间:2016-06-02 08:55:50

标签: matlab if-statement histogram

我有一系列有序点(X,Y,Z),我想绘制3D直方图,有什么建议吗?

我试图通过本教程http://www.mathworks.com/help/stats/hist3.html来做,但点在这里是随机的,并作为一个函数呈现。我的例子比较容易,因为我已经知道了要点。

此外,根据Z坐标的数值,我希望以不同的方式对其进行着色。例如。最大值 - 绿色,最小值 - 红色。与此情况类似Conditional coloring of histogram graph in MATLAB,仅限于3D。

所以,如果我有一系列要点:

X = [32 64 32 12 56 76 65]
Y = [160 80 70 48 90 80 70]
Z = [80 70 90 20 45 60 12]

你能帮助我使用条件着色的3D直方图代码吗?

到目前为止代码看起来像这样:

X = [32 64 32 12 56 76 65];
Y= [160 80 70 48 90 80 70];
Z= [80 70 90 20 45 60 12];
A = full( sparse(X',Y',Z'));
figure; 
h = bar3(A); % get handle to graphics
for k=1:numel(h), 
    z=get(h(k),'ZData'); % old data - need for its NaN pattern
    nn = isnan(z); 
    nz = kron( A(:,k),ones(6,4) ); % map color to height 6 faces per data point 
    nz(nn) = NaN; % used saved NaN pattern for transparent faces 
    set(h(k),'CData', nz); % set the new colors
end
colorbar;

现在我只需清除线条并设计图表,使其看起来很有用。但是如果没有整个网格在0级别上制作一个bar3怎么可能呢?

1 个答案:

答案 0 :(得分:2)

Based on this answer,您需要做的就是重新排列数据以匹配该答案的Z格式。之后您可能需要删除边线并可能清除零高度条。

% Step 1: rearrange your data
X = [32 64 32 12 56 76 65];
Y= [160 80 70 48 90 80 70];
Z= [80 70 90 20 45 60 12];
A = full( sparse(X',Y',Z'));

% Step 2: Use the code from the link to plot the 3D histogram
figure; 
h = bar3(A); % get handle to graphics

set(h,'edgecolor','none'); % Hopefully this will remove the lines (from https://www.mathworks.com/matlabcentral/newsreader/view_thread/281581)

for k=1:numel(h), 
    z=get(h(k),'ZData'); % old data - need for its NaN pattern
    nn = isnan(z); 
    nz = kron( A(:,k),ones(6,4) ); % map color to height 6 faces per data point 
    nz(nn) = NaN; % used saved NaN pattern for transparent faces 

    nz(nz==0) = NaN; % This bit makes all the zero height bars have no colour

    set(h(k),'CData', nz); % set the new colors. Note in later versions you can do h(k).CData = nz
end

colorbar;

enter image description here

相关问题