利用plt.hist()

  1. import matplotlib.pylab as plt
  2. %matplotlib inline
  3. plt.figure(figsize=(21, 12))
  4. plt.hist(x, bins=50)
  5. # plt.hist(df['title'].apply(lambda x: len(x)), bins=50)
  6. plt.grid()
  7. plt.savefig('distribution.png')

模块化:

  1. def plot_data_distribution(value_list, figsize=(21, 12), bins=50, fout=None):
  2. plt.figure(figsize=figsize)
  3. plt.hist(value_list, bins=bins)
  4. plt.xticks(range(int(max(value_list) - min(value_list))))
  5. # plt.xticks([i * 0.01 for i in range(0, 110, 5)])
  6. # plt.xticks([0.1 * x for x in range(11)])
  7. # plt.xlim(0, 1)
  8. plt.grid()
  9. if fout:
  10. plt.savefig(fout)
  11. else:
  12. plt.show()

把多个数据分布显示在一个直方图表中对比:

  1. def plot_multi_data_distribution(value_list1, value_list2, figsize=(21, 12), bins=50, fout=None):
  2. plt.figure(figsize=figsize)
  3. plt.hist([value_list1, value_list2], bins=bins, color=["r", "b"], label=["value_list1", "value_list2"])
  4. # plt.xticks(range(int(max(value_list) - min(value_list))))
  5. plt.grid()
  6. plt.legend(loc=1)
  7. plt.xlabel("probability")
  8. plt.ylabel("count")
  9. plt.title("data distribution")
  10. if fout:
  11. plt.savefig(fout)
  12. else:
  13. plt.show()
  1. def gen_colors(num):
  2. # colors = ["blue", "red", "green", "black", "brown", "orange", "gray", "purple", "salmon", "hotpink",
  3. # "#222222", "#444444", "#666666", "#888888", "#AAAAAA", "#CCCCCC", "#EEEEEE",
  4. # "#111111", "#333333", "#555555", "#777777", "#999999"]
  5. colors = ["black", "darkgrey", "red", "darkorange", "brown", "darkgoldenrod", "yellow", "lightgreen", "green", "lime",
  6. "cyan", "deepskyblue", "dodgerblue", "cornflowerblue", "blue", "purple", "fuchsia", "lightpink"]
  7. return colors[:num]
  8.  
  9. def plot_multi_data_distribution(value_lists, names, figsize=(21, 12), bins=50, fout=None):
  10. plt.figure(figsize=figsize)
  11. plt.hist(value_lists, bins=bins, color=gen_colors(num=len(value_lists)), label=names)
  12. # plt.xticks(range(int(max(value_list) - min(value_list))))
  13. plt.xticks([i * 0.01 for i in range(0, 110, 5)])
  14. plt.grid()
  15. plt.legend(loc=1)
  16. plt.xlabel("probability")
  17. plt.ylabel("count")
  18. plt.title("data distribution")
  19. if fout:
  20. plt.savefig(fout)
  21. else:
  22. plt.show()

生成渐变色:

  1. def gen_colors(num, base_color="#1000FF", interval=1600000):
  2. base = int(base_color.replace("#", "0x"), 16)
  3. # return ["blue", "red", "green", "black", "brown", "orange", "gray", "purple", "hotpink", "salmon"][:num]
  4. # return ["#0000FF", "#00FF00", "#FF0000", "#00FFFF", "#FF00FF", "#FFFF00", "#000000",
  5. # "#666666", "#660000", "#666600", "#660066"][:num]
  6. return ["#" + hex(base + (i * interval))[2:] for i in range(num)]

例如下面这种(看到眼花哈哈哈):

还是选择几种比较特别的颜色会方便看一些.

Matplotlib uses a dictionary from its colors.py module. To print the names use:

  1. # python2:
  2.  
  3. import matplotlib
  4. for name, hex in matplotlib.colors.cnames.iteritems():
  5. print(name, hex)
  6.  
  7. # python3:
  8.  
  9. import matplotlib
  10. for name, hex in matplotlib.colors.cnames.items():
  11. print(name, hex)

This is the complete dictionary:

  1. cnames = {
  2. 'aliceblue': '#F0F8FF',
  3. 'antiquewhite': '#FAEBD7',
  4. 'aqua': '#00FFFF',
  5. 'aquamarine': '#7FFFD4',
  6. 'azure': '#F0FFFF',
  7. 'beige': '#F5F5DC',
  8. 'bisque': '#FFE4C4',
  9. 'black': '#000000',
  10. 'blanchedalmond': '#FFEBCD',
  11. 'blue': '#0000FF',
  12. 'blueviolet': '#8A2BE2',
  13. 'brown': '#A52A2A',
  14. 'burlywood': '#DEB887',
  15. 'cadetblue': '#5F9EA0',
  16. 'chartreuse': '#7FFF00',
  17. 'chocolate': '#D2691E',
  18. 'coral': '#FF7F50',
  19. 'cornflowerblue': '#6495ED',
  20. 'cornsilk': '#FFF8DC',
  21. 'crimson': '#DC143C',
  22. 'cyan': '#00FFFF',
  23. 'darkblue': '#00008B',
  24. 'darkcyan': '#008B8B',
  25. 'darkgoldenrod': '#B8860B',
  26. 'darkgray': '#A9A9A9',
  27. 'darkgreen': '#006400',
  28. 'darkkhaki': '#BDB76B',
  29. 'darkmagenta': '#8B008B',
  30. 'darkolivegreen': '#556B2F',
  31. 'darkorange': '#FF8C00',
  32. 'darkorchid': '#9932CC',
  33. 'darkred': '#8B0000',
  34. 'darksalmon': '#E9967A',
  35. 'darkseagreen': '#8FBC8F',
  36. 'darkslateblue': '#483D8B',
  37. 'darkslategray': '#2F4F4F',
  38. 'darkturquoise': '#00CED1',
  39. 'darkviolet': '#9400D3',
  40. 'deeppink': '#FF1493',
  41. 'deepskyblue': '#00BFFF',
  42. 'dimgray': '#696969',
  43. 'dodgerblue': '#1E90FF',
  44. 'firebrick': '#B22222',
  45. 'floralwhite': '#FFFAF0',
  46. 'forestgreen': '#228B22',
  47. 'fuchsia': '#FF00FF',
  48. 'gainsboro': '#DCDCDC',
  49. 'ghostwhite': '#F8F8FF',
  50. 'gold': '#FFD700',
  51. 'goldenrod': '#DAA520',
  52. 'gray': '#808080',
  53. 'green': '#008000',
  54. 'greenyellow': '#ADFF2F',
  55. 'honeydew': '#F0FFF0',
  56. 'hotpink': '#FF69B4',
  57. 'indianred': '#CD5C5C',
  58. 'indigo': '#4B0082',
  59. 'ivory': '#FFFFF0',
  60. 'khaki': '#F0E68C',
  61. 'lavender': '#E6E6FA',
  62. 'lavenderblush': '#FFF0F5',
  63. 'lawngreen': '#7CFC00',
  64. 'lemonchiffon': '#FFFACD',
  65. 'lightblue': '#ADD8E6',
  66. 'lightcoral': '#F08080',
  67. 'lightcyan': '#E0FFFF',
  68. 'lightgoldenrodyellow': '#FAFAD2',
  69. 'lightgreen': '#90EE90',
  70. 'lightgray': '#D3D3D3',
  71. 'lightpink': '#FFB6C1',
  72. 'lightsalmon': '#FFA07A',
  73. 'lightseagreen': '#20B2AA',
  74. 'lightskyblue': '#87CEFA',
  75. 'lightslategray': '#778899',
  76. 'lightsteelblue': '#B0C4DE',
  77. 'lightyellow': '#FFFFE0',
  78. 'lime': '#00FF00',
  79. 'limegreen': '#32CD32',
  80. 'linen': '#FAF0E6',
  81. 'magenta': '#FF00FF',
  82. 'maroon': '#800000',
  83. 'mediumaquamarine': '#66CDAA',
  84. 'mediumblue': '#0000CD',
  85. 'mediumorchid': '#BA55D3',
  86. 'mediumpurple': '#9370DB',
  87. 'mediumseagreen': '#3CB371',
  88. 'mediumslateblue': '#7B68EE',
  89. 'mediumspringgreen': '#00FA9A',
  90. 'mediumturquoise': '#48D1CC',
  91. 'mediumvioletred': '#C71585',
  92. 'midnightblue': '#191970',
  93. 'mintcream': '#F5FFFA',
  94. 'mistyrose': '#FFE4E1',
  95. 'moccasin': '#FFE4B5',
  96. 'navajowhite': '#FFDEAD',
  97. 'navy': '#000080',
  98. 'oldlace': '#FDF5E6',
  99. 'olive': '#808000',
  100. 'olivedrab': '#6B8E23',
  101. 'orange': '#FFA500',
  102. 'orangered': '#FF4500',
  103. 'orchid': '#DA70D6',
  104. 'palegoldenrod': '#EEE8AA',
  105. 'palegreen': '#98FB98',
  106. 'paleturquoise': '#AFEEEE',
  107. 'palevioletred': '#DB7093',
  108. 'papayawhip': '#FFEFD5',
  109. 'peachpuff': '#FFDAB9',
  110. 'peru': '#CD853F',
  111. 'pink': '#FFC0CB',
  112. 'plum': '#DDA0DD',
  113. 'powderblue': '#B0E0E6',
  114. 'purple': '#800080',
  115. 'red': '#FF0000',
  116. 'rosybrown': '#BC8F8F',
  117. 'royalblue': '#4169E1',
  118. 'saddlebrown': '#8B4513',
  119. 'salmon': '#FA8072',
  120. 'sandybrown': '#FAA460',
  121. 'seagreen': '#2E8B57',
  122. 'seashell': '#FFF5EE',
  123. 'sienna': '#A0522D',
  124. 'silver': '#C0C0C0',
  125. 'skyblue': '#87CEEB',
  126. 'slateblue': '#6A5ACD',
  127. 'slategray': '#708090',
  128. 'snow': '#FFFAFA',
  129. 'springgreen': '#00FF7F',
  130. 'steelblue': '#4682B4',
  131. 'tan': '#D2B48C',
  132. 'teal': '#008080',
  133. 'thistle': '#D8BFD8',
  134. 'tomato': '#FF6347',
  135. 'turquoise': '#40E0D0',
  136. 'violet': '#EE82EE',
  137. 'wheat': '#F5DEB3',
  138. 'white': '#FFFFFF',
  139. 'whitesmoke': '#F5F5F5',
  140. 'yellow': '#FFFF00',
  141. 'yellowgreen': '#9ACD32'}

上面对应的颜色:

另外的显示方式:

装了seaborn扩展的话,在字典seaborn.xkcd_rgb中包含所有的xkcd crowdsourced color names。如下:

  1. plt.plot([1,2], lw=4, c=seaborn.xkcd_rgb['baby poop green'])

所有颜色如下:

更多详细参考:

https://www.cnblogs.com/jerrybaby/p/6118236.html

http://baijiahao.baidu.com/s?id=1595822592180003842&wfr=spider&for=pc

http://baijiahao.baidu.com/s?id=1576521879286470276&wfr=spider&for=pc

https://blog.csdn.net/yywan1314520/article/details/50818471

https://www.cnblogs.com/laoniubile/p/5893286.html

python颜色设置

python中matplotlib的颜色及线条控制

Python matplotlib 数据分布的更多相关文章

  1. python matplotlib 中文显示参数设置

    python matplotlib 中文显示参数设置 方法一:每次编写代码时进行参数设置 #coding:utf-8import matplotlib.pyplot as pltplt.rcParam ...

  2. python matplotlib plot 数据中的中文无法正常显示的解决办法

    转发自:http://blog.csdn.net/laoyaotask/article/details/22117745?utm_source=tuicool python matplotlib pl ...

  3. python matplotlib画图产生的Type 3 fonts字体没有嵌入问题

    ScholarOne's 对python matplotlib画图产生的Type 3 fonts字体不兼容,更改措施: 在程序中添加如下语句 import matplotlib matplotlib. ...

  4. 使用Python matplotlib做动态曲线

    今天看到“Python实时监控CPU使用率”的教程: https://www.w3cschool.cn/python3/python3-ja3d2z2g.html 自己也学习如何使用Python ma ...

  5. python matplotlib 中文显示乱码设置

    python matplotlib 中文显示乱码设置 原因:是matplotlib库中没有中文字体.1 解决方案:1.进入C:\Anaconda64\Lib\site-packages\matplot ...

  6. Python - matplotlib 数据可视化

    在许多实际问题中,经常要对给出的数据进行可视化,便于观察. 今天专门针对Python中的数据可视化模块--matplotlib这块内容系统的整理,方便查找使用. 本文来自于对<利用python进 ...

  7. 转:使用 python Matplotlib 库 绘图 及 相关问题

     使用 python Matplotlib 库绘图      转:http://blog.csdn.net/daniel_ustc/article/details/9714163 Matplotlib ...

  8. python+matplotlib 绘制等高线

    python+matplotlib 绘制等高线 步骤有七: 有一个m*n维的矩阵(data),其元素的值代表高度 构造两个向量:x(1*n)和y(1*m).这两个向量用来构造网格坐标矩阵(网格坐标矩阵 ...

  9. 安装python Matplotlib 库

    转:使用 python Matplotlib 库 绘图 及 相关问题  使用 python Matplotlib 库绘图      转:http://blog.csdn.net/daniel_ustc ...

随机推荐

  1. 总结的一些json格式和对象/String/Map/List等的互转工具类

    总结的一些json格式和对象/String/Map/List等的互转工具类,有需要的可以看看,需要引入jackson-core-asl-1.7.1.jar.jackson-jaxrs-1.7.1.ja ...

  2. 修改Linux登录提示信息

    佛祖保佑式 cat >> /etc/profile.d/message.sh << END echo -e "\033[33;40;5m _ooOoo_ O88888 ...

  3. An Example for Javascript Function Scoping and Closure

    1. An Real World Example In the patron detail page of the CRM system I'm working with, there’re larg ...

  4. TCP状态切换流程

    enum { /* * Description of States: * * TCP_SYN_SENT sent a connection request, waiting for ack * * T ...

  5. mysql_fetch_assoc查询多行数据

    每次从查询结果中返回一行数据,作为关联数组,类似于一个游标,第一次是返回第一行,第二次迭代就是第二行,以此类推 如果返回多行,使用如下方法就可以了 while($row = $db->fetch ...

  6. 返回值过长时被nginx截断的解决办法

    今天在写接口时碰到了这个问题,返回json格式的数据,但是被截断了经过排查,才发现是数据过大超出缓冲区最大容量,而将数据写入临时文件时又没有权限,所以再返回时,超出缓冲区的数据将丢失解决方法:给fas ...

  7. Springboot依赖注入笔记

    结合Autowired和Service注解 public interface IUser { void say(); } @Service public class Student implement ...

  8. MySQL 存储过程参数用法 in, out, inout

    MySQL 存储过程参数有三种类型:in.out.inout.它们各有什么作用和特点呢? 一.MySQL 存储过程参数(in) MySQL 存储过程 “in” 参数:跟 C 语言的函数参数的值传递类似 ...

  9. nginx 日志搜集解决方案

    # nginx 日志搜集解决方案 ## 系统环境描述 ``` java8 logstash --监控nginx日志文件 ``` ## 技术描述 ``` 通过logstash监控nginx access ...

  10. 理解OAuth 2.0授权

    一.什么是OAuth 二.什么场景下会用到OAuth授权 三.OAuth 2.0中的4个成员 四.OAuth 2.0授权流程 五.OAuth 2.0授权模式 1.    authorization c ...