matlab rgb三维直方图,MATLAB小技巧之十:利用MATLAB绘制三维彩色柱状图[转载]

MATLAB自带有绘制三维柱状图的函数bar3,有版友反映不好控制柱子的颜色,特别是bar3不能绘制渐变色的柱子,并且bar3不能根据用户指定的散点数据绘制柱状图。为此,我编写了一个函数(scatterbar),用来绘制带渐变色的三维彩色柱状图。下面给出示例。

【例】根据散点数据绘制三维彩色柱状图

代码1:

[x,y] = meshgrid(-6:6,-3:0.5:3);

z = mvnpdf([x(:),y(:)],[0,0],[4,0;0,1]);

scatterbar(x,y,z)

效果图1:

a4c26d1e5885305701be709a3d33442f.png

代码2:

x = 0:0.1:2*pi;

y = sin(x);

scatterbar(x,y,y,100);

效果图2:

a4c26d1e5885305701be709a3d33442f.png

代码3:

t = 0:0.1:2*pi;

x = cos(t);

y = sin(t);

z = abs(y);

scatterbar(x,y,z,100);

效果图3:

a4c26d1e5885305701be709a3d33442f.png

scatterbar函数源代码:

function scatterbar(x,y,z,scale)

%  根据散点数据绘制3维彩色柱状图

%  scatterbar(x,y,z,scale)

x,y,z是实值数组,用来指定柱子顶面中心点三维坐标。

%  scale是大于0的标量,用来指定柱子的粗细,scale越大,柱子越细,默

%  认情况下根据坐标点自动计算柱子的粗细。

%

%  CopyRight:xiezhh(谢中华)

%  2011.10.31

%  Example:

%  [x,y] =

meshgrid(-6:6,-3:0.5:3);

%  z =

mvnpdf([x(:),y(:)],[0,0],[4,0;0,1]);

%  scatterbar(x,y,z)

%  scatterbar(x,y,z,50);

% 输入参数类型判断

if nargin < 3

error('至少需要三个输入参数');

end

if ~isreal(x) || ~isreal(y) || ~isreal(z)

error('前三个输入应为实值数组');

end

% 提取x,y,z等长部分的元素

x = x(:);

y = y(:);

z = z(:);

n = min([numel(x) numel(y) numel(z)]);

x = x(1:n);

y = y(1:n);

z = z(1:n);

% 计算极差和差分值

rx = range(x);

ry = range(y);

dx = abs(diff(x));

dx = min(dx(dx>0));

dy = abs(diff(y));

dy = min(dy(dy>0));

% 自动计算柱子的粗细

if nargin == 3

if ~isempty(dx)

hx = dx/2;

else

hx = 0.5;

end

if ~isempty(dy)

hy = dy/2;

else

hy = 0.5;

end

end

% 根据用户输入参数scale计算柱子的粗细

if nargin == 4

if ~isreal(scale) || scale

< 0

error('第四个输入应为正的标量');

end

if rx == 0 && ry ==

0

rx = 0.5*scale;

ry = rx;

elseif rx == 0 || ry == 0

rx = max(rx,ry);

ry = rx;

end

hx = rx/scale;

hy = ry/scale;

end

% 通过循环绘制三维彩色柱状图

figure

hold on

Xp = [];

Yp = [];

Zp = [];

for i = 1:n

[xp,yp,zp] =

Vertices(x(i),y(i),z(i));

Xp = [Xp;xp];

Yp = [Yp;yp];

Zp = [Zp;zp];

end

%通过surf函数生成彩色的立方体盒子

h = surf(Xp,Yp,Zp,Zp,'FaceColor','interp');

%set(h,'FaceAlpha',0.25);  %设置立方体盒子透明度

grid on

view(3)

hold off

%--------------------------------------------------

% 求柱子顶点的子函数

%--------------------------------------------------

function [xp,yp,zp] = Vertices(x,y,z)

% 由长方体底面中心坐标求顶点坐标

xp = [x-hx x-hx x+hx x+hx

x-hx

x-hx x-hx x+hx x+hx x-hx

x-hx x-hx x+hx x+hx x-hx

x-hx x-hx x+hx x+hx x-hx

x  x  x

x  x

NaN  NaN

NaN  NaN

NaN];

yp = [y-hy y+hy y+hy y-hy

y-hy

y-hy y+hy y+hy y-hy y-hy

y-hy y+hy y+hy y-hy y-hy

y-hy y+hy y+hy y-hy y-hy

y  y  y

y  y

NaN  NaN

NaN  NaN

NaN];

zp =

[repmat(linspace(0,z,4)',[1,5]);z z z z z;NaN NaN NaN NaN NaN];

end

end

你可能感兴趣的:(matlab,rgb三维直方图)