matlab bar3 函数,MATLAB中bar3()函数画柱状图时按高度显示颜色

在利用MATLAB的bar3()函数画一个矩阵的柱状图时,如何改变显示的颜色,让它根据数据大小的不同显示不同的颜色,比如数据越大颜色越深,而不是bar3内置的颜色那样沿x轴或y轴渐变。

M=rand(30,20);

figure

subplot(1,2,1)

h=bar3(M);

for n=1:numel(h)

cdata=get(h(n),'zdata');

set(h(n),'cdata',cdata,'facecolor','interp')

end

subplot(1,2,2)

h=bar3(M);

for n=1:numel(h)

cdata=get(h(n),'zdata');

cdata=repmat(max(cdata,[],2),1,4);

set(h(n),'cdata',cdata,'facecolor','flat')

end

Color 3-D Bars by Height

http://cn.mathworks.com/help/matlab/creating_plots/color-3-d-bars-by-height-1.html

This example shows how to modify a 3-D bar plot by coloring each bar according to its height.

Create a 3-D bar graph of data from the magic function. Return the surface objects used to create the bar graph in array b. Add a colorbar to the graph.

Z = magic(5);

b = bar3(Z);

colorbar

For each surface object, get the array of z-coordinates from the ZData property. Use the array to set the CData property, which defines the vertex colors. Interpolate the face colors by setting the FaceColor properties of the surface objects to 'interp'.

Note: Starting in R2014b, you can use dot notation to query and set properties. If you are using an earlier release, use the get and set functions instead, such as zdata = get(b(k),'ZData').

for k = 1:length(b)

zdata = b(k).ZData;

b(k).CData = zdata;

b(k).FaceColor = 'interp';

end

The height of each bar determines its color. You can estimate the bar heights by comparing the bar colors to the colorbar.

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值