如何在MATLAB中的bar3图中隐藏零值 [英] How to hide zero values in bar3 plot in MATLAB
问题描述
我有一个用bar3 plot命令生成的2D直方图(绘图是3D-并排绘制的几个直方图).但是,所有零值在x-y平面中均显示为正方形.有什么方法可以防止MATLAB显示值吗?我已经尝试过用NaN替换所有零,但是它并没有改变情节.这是我一直在尝试的代码:
I've got a 2-D histogram (the plot is 3D - several histograms graphed side by side) that I've generated with the bar3 plot command. However, all the zero values show up as flat squares in the x-y plane. Is there a way I can prevent MATLAB from displaying the values? I already tried replacing all zeros with NaNs, but it didn't change anything about the plot. Here's the code I've been experimenting with:
x1=normrnd(50,15,100,1); %generate random data to test code
x2=normrnd(40,13,100,1);
x3=normrnd(65,12,100,1);
low=min([x1;x2;x3]);
high=max([x1;x2;x3]);
y=linspace(low,high,(high-low)/4); %establish consistent bins for histogram
z1=hist(x1,y);
z2=hist(x2,y);
z3=hist(x3,y);
z=[z1;z2;z3]';
bar3(z)
如您所见,图上有很多零值.关闭数字并用NaN替换零后重新绘图似乎没有任何改变:
As you can see, there are quite a few zero values on the plot. Closing the figure and re-plotting after replacing zeros with NaNs seems to change nothing:
close
z(z==0)=NaN;
bar3(z)
推荐答案
一种解决方案是修改 bar3
返回句柄:
One solution is to modify the graphics objects created by bar3
. First, you have to get the handles returned from bar3
:
h = bar3(z);
在您的情况下,h
将是一个由3个元素组成的手柄矢量,每组彩色条对应一个.然后,以下代码应使计数为零的垃圾箱不可见:
In your case, h
will be a 3-element vector of handles, one for each set of colored bars. The following code should then make the bins with counts of zero invisible:
for i = 1:numel(h)
index = logical(kron(z(:, i) == 0, ones(6, 1)));
zData = get(h(i), 'ZData');
zData(index, :) = nan;
set(h(i), 'ZData', zData);
end
这是一个插图(带有强制性的手绘圈):
And here's an illustration (with obligatory free-hand circles):
如果您的垃圾箱计数向量是N-by-1
,则 bar3
将绘制6*N
矩形补丁(即每个垃圾箱的一个长方体的6个面).因此,由于每个矩形面都有4个角,因此h
中每组补丁对象的'ZData'
属性将为(6*N)-by-4
.因此,每个'ZData'
属性的6行簇都是一个bin的6个面的一组z坐标.
If your vector of bin counts is N-by-1
, then bar3
will plot 6*N
rectangular patches (i.e. the 6 faces of a cuboid for each bin). The 'ZData'
property for each set of patch objects in h
will therefore be (6*N)-by-4
, since there are 4 corners for each rectangular face. Each cluster of 6 rows of the 'ZData'
property is therefore a set of z-coordinates for the 6 faces of one bin.
以上代码首先创建一个逻辑矢量,该矢量在所有垃圾箱计数等于0的位置处,然后使用 nan
表示空箱的补丁.这将导致补丁无法渲染.
The above code first creates a logical vector with ones everywhere the bin count equals 0, then replicates each element of this vector 6 times using the kron
function. This becomes an index for the rows of the 'ZData'
property, and this index is used to set the z-coordinates to nan
for the patches of empty bins. This will cause the patches to not be rendered.
这里是代码的略微修改版本,通过从 bar3
.我还将代码包装在一个函数中(没有错误和输入检查):
Here's a slightly modified version of the code that makes it more general by fetching the bar height from the 'ZData'
property of the plotted bars, so all that's needed for it to work are the handles returned from bar3
. I've also wrapped the code in a function (sans error and input checking):
function remove_empty_bars(hBars)
for iSeries = 1:numel(hBars)
zData = get(hBars(iSeries), 'ZData'); % Get the z data
index = logical(kron(zData(2:6:end, 2) == 0, ones(6, 1))); % Find empty bars
zData(index, :) = nan; % Set the z data for empty bars to nan
set(hBars(iSeries), 'ZData', zData); % Update the graphics objects
end
end
这篇关于如何在MATLAB中的bar3图中隐藏零值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!