Mathematica中文论坛-非官方

标题: 豆粑粑 matlab bar(hist)根据值(概率)设置颜色 [打印本页]

作者: meatball1982    时间: 2018-5-17 10:48
标题: 豆粑粑 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
  1. clear all
  2. clc
  3. clf

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


  7. %% main
  8. % generate bar data
  9. a=randn(5000,1);
  10. [hi_va,hi_bi]=hist(a,linspace(-4,4,40));
  11. hi_va=hi_va./sum(hi_va);
  12. n_bar = length(hi_bi);
  13. hi_max = max(hi_va);

  14. % generate color
  15. n_col=100;
  16. col_mm = colormap(jet(n_col));

  17. % plot bar each time
  18. hold on
  19. for i =1:n_bar

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

  24. end

  25. % other setting
  26. colormap(col_mm)
  27. caxis([0 hi_max])
  28. colorbar
  29. box on
  30. grid on

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

  34. %% 2014a and before
  35. % a=randn(5000,1);
  36. % [hi_va,hi_bi]=hist(a,linspace(-4,4,40));
  37. % hi_va=hi_va./sum(hi_va);
  38. % n_bar = length(hi_bi);
  39. % hi_max = max(hi_va);
  40. %
  41. % % generate color
  42. % n_col=100;
  43. % col_mm = colormap(jet(n_col));
  44. % h=bar(hi_bi,hi_va);
  45. % ch = get(h,'Children');
  46. % fvd = get(ch,'Faces');
  47. % fvcd = get(ch,'FaceVertexCData');
  48. % for row = 1:n_bar
  49. %      fvcd(fvd(row,:)) = hi_va(row);
  50. % end
  51. %
  52. % set(ch,'FaceVertexCData',fvcd)
  53. % caxis([0 hi_max])
  54. %
  55. % colorbar
  56. % grid on






  57. %% logs
  58. % mod : 17-May-2018 10:25:33

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








欢迎光临 Mathematica中文论坛-非官方 (http://ilovemathematica.com/) Powered by Discuz! X3.2