简体   繁体   English

MATLAB:在保留边线的同时去除bar3图中零的颜色

[英]MATLAB: remove color of zero in bar3 plot while keeping the edge lines

I am plotting some data with bar3 in Matlab. 我在Matlab中用bar3绘制一些数据。

In the plot, I would like to make the zero value blank, but I want to keep the black edge of each zero cell (ie the black line around the cell). 在图中,我想将零值留为空白,但我想保留每个零单元格的黑色边缘(即单元格周围的黑线)。

So far I could only find an answer which totally removes the data in a null cell: both the bar and its edge... cf How to hide zero values in bar3 plot in MATLAB . 到目前为止,我只能找到一个完全删除空单元格中数据的答案:条形图及其边缘... cf 如何在MATLAB中的bar3图中隐藏零值

Has anybody an idea to do that? 有没有人想这样做?

Thank you in advance. 先感谢您。

Following my comment, here is a solution. 根据我的评论,这是一个解决方案。 To sum up simply, you cannot change the color of only one bar drawn by bar3 . 概括地说,您不能仅更改由bar3绘制的一个条的bar3

Instead of displaying each bar by hand, my solution is to modify the code of bar3 in order to draw each bar independently, which you can read freely. 我的解决方案不是手动显示每个条形图,而是修改bar3的代码以独立绘制每个条形图,您可以自由阅读这些条形图。 This is rather simple when you analyse the code of bar3 : each bar is the graphic representation of 6*4 data matrices. 当您分析bar3的代码时,这相当简单:每个条形图都是6 * 4数据矩阵的图形表示。 The block of code in question is the following: 有问题的代码块如下:

for i=1:size(yy,2)/4
    h = [h,surface('xdata',xx+x(i),...
            'ydata',yy(:,(i-1)*4+(1:4)), ...
            'zdata',zz(:,(i-1)*4+(1:4)),...
            'cdata',i*cc, ...
            'FaceColor',facec,...
            'EdgeColor',edgec,...
            'tag','bar3',...
            'parent',cax)];
end

As you see, surface is called on every data column. 如您所见,在每个数据列上都会调用surface To call surface on each element, you can modify the code as follows: 要在每个元素上调用surface ,可以如下修改代码:

for i=1:size(yy,2)/4
    for j=1:size(yy,1)/6
        h = [h,surface('xdata',xx((j-1)*6+(1:5),:)+x(i),...
                'ydata',yy((j-1)*6+(1:5),(i-1)*4+(1:4)), ...
                'zdata',zz((j-1)*6+(1:5),(i-1)*4+(1:4)),...
                'cdata',i*cc((j-1)*6+(1:5),:), ...
                'FaceColor',facec,...
                'EdgeColor',edgec,...
                'tag','bar3',...
                'parent',cax)];
    end
end

You cannot modify the original bar3 , so let's save it as bar3_mod . 您无法修改原始的bar3 ,因此将其保存为bar3_mod

With this done, if you refer to the doc article about Color 3-D Bars by Height , it is now really simple to make bars of zero height transparent. 完成此操作后,如果您参考有关按高度设置颜色3-D条形的文档文章,现在使零高度的条形透明变得非常简单。 Before this, remember that the height of a bar you get with a get on the handle of one bar is described by a 5*4 matrix of the form: 在此之前,请记住,你有得到一个栏的高度get的是由以下形式的5×4矩阵描述一个栏的手柄:

NaN     0     0   NaN
  0     Z     Z     0
  0     Z     Z     0
NaN     0     0   NaN
NaN     0     0   NaN

So you must only test the value of the element at (2,2) and change the color as you want. 因此,您仅需测试(2,2)处元素的值并根据需要更改颜色。 In your case, it's quite simple to derive the code given in the linked page: 在您的情况下,派生链接页面中给出的代码非常简单:

h = bar3_mod(Z);
for k = 1:length(h)
    zdata = get(h(k),'ZData');
    if zdata(2,2)==0
        set(h(k),'CData',zdata,'FaceColor','none');
    end
end

I've tested it on an example, with magic(5) as input and making the bar with a height of 1 transparent: 我已经在示例中对其进行了测试,并使用magic(5)作为输入并使高度为1的条透明:

bar3_mod +透明条

EDIT 编辑

Like bar3 , there is one color per data column. bar3一样,每个数据列只有一种颜色。 If you want to color each bar according to it's value, you can either modify the code of bar3_mod or add a few more instructions when you make the specified bars transparent. 如果要根据每个栏的值给它们上色,则可以修改bar3_mod的代码,或者在使指定的栏透明时添加更多说明。

1st solution: quite simple to change the for-loop: 第一种解决方案:更改for循环非常简单:

for i=1:size(yy,2)/4
    for j=1:size(yy,1)/6
        h = [h,surface('xdata',xx((j-1)*6+(1:5),:)+x(i),...
                'ydata',yy((j-1)*6+(1:5),(i-1)*4+(1:4)), ...
                'zdata',zz((j-1)*6+(1:5),(i-1)*4+(1:4)),...
                'cdata',zz((j-1)*6+2,(i-1)*4+2)*cc((j-1)*6+(1:5),:), ... % here is the modification
                'FaceColor',facec,...
                'EdgeColor',edgec,...
                'tag','bar3',...
                'parent',cax)];
    end
end

2nd solution: just add a else case where you affect the new color data: 第二种解决方案:添加else情况,影响新的颜色数据:

for k = 1:length(h)
    zdata = get(h(k),'ZData');
    if zdata(2,2)==0
        set(h(k),'CData',zdata,'FaceColor','none');
    else
        set(h(k),'CData',ones(size(zdata))*zdata(2,2));
    end
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM