如何在 MATLAB 中的 bar3 图中隐藏零值 [英] How to hide zero values in bar3 plot in MATLAB

查看:73
本文介绍了如何在 MATLAB 中的 bar3 图中隐藏零值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用 bar3 plot 命令生成的 2-D 直方图(该图是 3D 的 - 几个并排绘制的直方图).但是,所有零值在 x-y 平面中都显示为平面正方形.有没有办法阻止 MATLAB 显示这些值?我已经尝试用 NaN 替换所有零,但它没有改变任何情节.这是我一直在试验的代码:

x1=normrnd(50,15,100,1);%生成随机数据来测试代码x2=normrnd(40,13,100,1);x3=normrnd(65,12,100,1);低=分钟([x1;x2;x3]);高=最大([x1;x2;x3]);y=linspace(low,high,(high-low)/4);%为直方图建立一致的binsz1=hist(x1,y);z2=hist(x2,y);z3=hist(x3,y);z=[z1;z2;z3]';bar3(z)

如您所见,图中有相当多的零值.用 NaN 替换零后关闭图形并重新绘制似乎没有任何改变:

关闭z(z==0)=NaN;bar3(z)

解决方案

一种解决方案是修改由

工作原理...

如果您的 bin 计数向量是 N-by-1,则 bar3 将绘制 6*N 个矩形块(即每个 bin 的长方体的 6 个面).'ZData'h 中每组补丁对象的属性因此将是 (6*N)-by-4,因为有每个矩形面有 4 个角.因此,'ZData' 属性的每个 6 行集群是一个 bin 的 6 个面的一组 z 坐标.

上面的代码首先创建一个逻辑向量,其中 bin 计数等于 0 的每个元素都是一个逻辑向量,然后使用 kron 函数.这将成为 'ZData' 属性行的索引,该索引用于将 z 坐标设置为 nan 用于空箱的补丁.这将导致无法渲染补丁.


这是代码的稍微修改版本,通过从 'ZData' 属性 绘制的条形,因此它工作所需的只是从 bar3.我还将代码包装在一个函数中(没有错误和输入检查):

function remove_empty_bars(hBars)对于 iSeries = 1:numel(hBars)zData = get(hBars(iSeries), 'ZData');% 获取 z 数据索引 = 逻辑 (kron(zData(2:6:end, 2) == 0,ones(6, 1)));% 查找空条zData(index, :) = nan;% 将空条的 z 数据设置为 nan设置(hBars(iSeries),'ZData',zData);% 更新图形对象结尾结尾

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)

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)

解决方案

One solution is to modify the graphics objects created by bar3. First, you have to get the handles returned from bar3:

h = bar3(z);

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):

How it works...

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.

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.


EDIT:

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屋!

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