Matlab绘制彩色渐变色柱状图

本文介绍如何使用Matlab绘制彩色渐变柱状图。通过调整柱状图的颜色来直观展示数据的大小变化,并提供两种实现方式:一种是根据数据值直接着色,另一种则采用渐变色填充,使图表更美观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
 
% 彩色柱状图
%% 用到的数据
n = 13;
Z = rand (n, 1 );
%% 默认图片
bar (Z );
%% 简单的作图
% 这个图根据数据列中值的大小着色。每列中的
% 值越大,颜色越突出
figure
h= bar (Z );
colormap ( summer (n ) );
ch = get (h, 'Children' );
fvd = get (ch, 'Faces' );
fvcd = get (ch, 'FaceVertexCData' );
[~, izs ] = sortrows (Z, 1 );
for i = 1:n
row = izs ( i );
fvcd (fvd (row,: ) ) = i;
end
set (ch, 'FaceVertexCData',fvcd )
%% 更加漂亮的图片
% 图片会以渐变的方式着色,效果非常不错
figure
h= bar (Z );
ch = get (h, 'Children' );
fvd = get (ch, 'Faces' );
fvcd = get (ch, 'FaceVertexCData' );
[zs, izs ] = sortrows (Z, 1 );
k = 128; % 准备生成128 *3 行的colormap
colormap ( summer (k ) ); % 这样会产生一个128 * 3的矩阵,分别代表[R G B]的值
% 检视数据
whos ch fvd fvcd zs izs
%
  Name       Size            Bytes  Class     Attributes
%
  ch         1x1                 double
  fvcd      66x1               528  double
  fvd       13x4               416  double
  izs       13x1               104  double
  zs        13x1               104  double
%
shading interp % Needed to graduate colors
for i = 1:n
color = floor (k* i/n ); % 这里用取整函数获得color在colormap中行
row = izs ( i ); % Look up actual row # in data
fvcd (fvd (row, 1 ) ) = 1; % Color base vertices 1st index
fvcd (fvd (row, 4 ) ) = 1;
fvcd (fvd (row, 2 ) ) = color; % Assign top vertices color
fvcd (fvd (row, 3 ) ) = color;
end
set (ch, 'FaceVertexCData', fvcd ); % Apply the vertex coloring
set (ch, 'EdgeColor', 'k' )
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值