我如何获得功能 bar3 和每个条形不同宽度的条形? [英] How I obtain bars with function bar3 and different widths for each bar?

查看:18
本文介绍了我如何获得功能 bar3 和每个条形不同宽度的条形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有代码:

values = [1.0 0.6 0.1;  0.0 1.0 0.3;  0.9 0.4 1.0];
h = bar3(values);
shading interp
for i = 1:length(h)
    % Get the ZData matrix of the current group
    zdata = get(h(i),'Zdata');
    set(h(i),'Cdata',zdata)
end
set(h,'EdgeColor','k')
view(-61, 68);
colormap cool
colorbar

这就是图的样子:

我想根据条的高度为每个条获得不同的宽度.

I want to obtain different widths for each bar dependent on the height of the bar.

我想要的看起来像http://www.sdtools.com/help/ii_mac.html.

等等http://www.sdtools.com/help/mac.gif

推荐答案

这有点难以理解,但是一旦你掌握了模式就很容易了.每个 h(i)'XData''YData' 属性是定义每个条形的 x 和 y 宽度的矩阵.这些矩阵的每组 6 行定义一个条形.所以诀窍是根据values修改'XData''YData'.

This was a little hard to figure out, but it's easy once you get the pattern. The 'XData' and 'YData' properties of each h(i) are matrices that define the x- and y- width of each bar. Each group of 6 rows of those matrices defines a bar. So the trick is to modify 'XData' and 'YData' according to values.

values = [1.0 0.6 0.1;  0.0 1.0 0.3;  0.9 0.4 1.0];
h = bar3(values);
m = max(values(:))*2; %// normalizing constant for bar width
shading interp
for i = 1:length(h)
    % Get the ZData matrix of the current group
    xdata = get(h(i),'Xdata');
    ydata = get(h(i),'Ydata');
    zdata = get(h(i),'Zdata');
    set(h(i),'Cdata',zdata)
    for k = 1:6:size(xdata,1)
        xdatak = xdata(k+(0:5),:);
        xdatak = round(xdatak)+sign(xdatak-round(xdatak))*values(ceil(k/6),i)/m;
        xdata(k+(0:5),:) = xdatak;
        ydatak = ydata(k+(0:5),:);
        ydatak = round(ydatak)+sign(ydatak-round(ydatak))*values(ceil(k/6),i)/m;
        ydata(k+(0:5),:) = ydatak;
    end
    set(h(i),'XData',xdata);
    set(h(i),'YData',ydata);
end
set(h,'EdgeColor','k')
view(-61, 68);
colormap cool
colorbar

请注意,上面的代码根据缩放线性大小(宽度).要缩放面积,只需使用的平方根:

Note that the above code scales linear size (width) according to values. To scale area just use the square root of values:

values = [1.0 0.6 0.1;  0.0 1.0 0.3;  0.9 0.4 1.0];
h = bar3(values);
svalues= sqrt(values);
m = max(svalues(:))*2; %// normalizing constant for bar width
shading interp
for i = 1:length(h)
    % Get the ZData matrix of the current group
    xdata = get(h(i),'Xdata');
    ydata = get(h(i),'Ydata');
    zdata = get(h(i),'Zdata');
    set(h(i),'Cdata',zdata)
    for k = 1:6:size(xdata,1)
        xdatak = xdata(k+(0:5),:);
        xdatak = round(xdatak)+sign(xdatak-round(xdatak))*svalues(ceil(k/6),i)/m;
        xdata(k+(0:5),:) = xdatak;
        ydatak = ydata(k+(0:5),:);
        ydatak = round(ydatak)+sign(ydatak-round(ydatak))*svalues(ceil(k/6),i)/m;
        ydata(k+(0:5),:) = ydatak;
    end
    set(h(i),'XData',xdata);
    set(h(i),'YData',ydata);
end
set(h,'EdgeColor','k')
view(-61, 68);
colormap cool
colorbar

在上述任何一个中,如果您希望所有条形高度相同,只需将第二行替换为

In either of the above, if you want all bars with equal height just replace the second line by

h = bar3(ones(size(values)));

或者,如果您更喜欢 2D 视图,请使用

Or if you prefer a 2D view, use

view(-90,90) %// view from above
axis equal %// set the same scale in x and y

这篇关于我如何获得功能 bar3 和每个条形不同宽度的条形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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