我有一系列有序的点(X,Y,Z),我想绘制3D直方图,有什么建议吗?
我正在尝试通过本教程http://www.mathworks.com/help/stats/hist3.html进行操作,但是此处的点是随机的,并作为函数呈现。我的例子比较容易,因为我已经知道要点了。
此外,根据Z坐标的数值,我想对它进行不同的着色。例如,最大值-绿色,最小值-红色。与这种情况类似,仅在3D模式下,在MATLAB中对直方图进行条件着色。
因此,如果我有一系列要点:
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呢?
根据此答案,您需要做的就是重新排列数据以匹配该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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句