Mathematica中文论坛-非官方

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 12798|回复: 0
打印 上一主题 下一主题

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

[复制链接]

529

主题

598

帖子

3010

积分

论坛元老

Rank: 8Rank: 8

积分
3010
跳转到指定楼层
楼主
发表于 2018-5-17 10:48:10 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 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



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|Mathematica中文论坛-非官方 ( 辽ICP备16001491号-1

GMT+8, 2024-4-19 13:21 , Processed in 0.131654 second(s), 28 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表