meatball1982 发表于 2018-5-17 10:48:10

豆粑粑 matlab bar(hist)根据值(概率)设置颜色

本帖最后由 meatball1982 于 2018-5-17 10:55 编辑

别人的问题,经常被问到,记录一下。
http://www.ilovematlab.com/forum ... 1&extra=#pid3399537
https://www.ilovematlab.cn/thread-545681-1-1.html




目的是根据bar的值,设置 bar的color.
思路,
不同的版本,实现方法不太一样。
2014a之后,是用bar一个一个的画。
2014a之前,是设置children的color.

contact me via :
meatball1982@163.com
clear all
clc
clf

%% outline
% http://www.ilovematlab.com/thread-545585-1-1.html
% mod : 17-May-2018 10:24:20


%% main
% generate bar data
a=randn(5000,1);
=hist(a,linspace(-4,4,40));
hi_va=hi_va./sum(hi_va);
n_bar = length(hi_bi);
hi_max = max(hi_va);

% generate color
n_col=100;
col_mm = colormap(jet(n_col));

% plot bar each time
hold on
for i =1:n_bar

    ind_col = floor((n_col-1)*hi_va(i)/hi_max)+1; % connect hi_va and colormap
    bar(hi_bi(i),hi_va(i),'edgecolor','none',...
      'facecolor',col_mm(ind_col,:),...
      'barwidth',0.18);

end

% other setting
colormap(col_mm)
caxis()
colorbar
box on
grid on

h=gcf;
fi_na='./fig_bar_dif_color'
fun_work_li_035_myfig_out(h,fi_na,3);

%% 2014a and before
% a=randn(5000,1);
% =hist(a,linspace(-4,4,40));
% hi_va=hi_va./sum(hi_va);
% n_bar = length(hi_bi);
% hi_max = max(hi_va);
%
% % generate color
% n_col=100;
% col_mm = colormap(jet(n_col));
% h=bar(hi_bi,hi_va);
% ch = get(h,'Children');
% fvd = get(ch,'Faces');
% fvcd = get(ch,'FaceVertexCData');
% for row = 1:n_bar
%      fvcd(fvd(row,:)) = hi_va(row);
% end
%
% set(ch,'FaceVertexCData',fvcd)
% caxis()
%
% colorbar
% grid on






%% logs
% mod : 17-May-2018 10:25:33

2014a之前的是根据这个帖子,注明一下。http://blog.sina.com.cn/s/blog_b1ffa5f10102vyc1.html



页: [1]
查看完整版本: 豆粑粑 matlab bar(hist)根据值(概率)设置颜色