zoukankan      html  css  js  c++  java
  • Python——使用matplotlib绘制柱状图

    Python——使用matplotlib绘制柱状图

    1、基本柱状图

              首先要安装matplotlib(http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot) 可以使用pip命令直接安装
    [python] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import matplotlib.pyplot as plt  
    3.   
    4. num_list = [1.5,0.6,7.8,6]  
    5. plt.bar(range(len(num_list)), num_list)  
    6. plt.show()  

    2、设置颜色

              
    [python] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import matplotlib.pyplot as plt  
    3.   
    4. num_list = [1.5,0.6,7.8,6]  
    5. plt.bar(range(len(num_list)), num_list,fc='r')  
    6. plt.show()  

    [cpp] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import matplotlib.pyplot as plt  
    3.   
    4. num_list = [1.5,0.6,7.8,6]  
    5. plt.bar(range(len(num_list)), num_list,color='rgb')  
    6. plt.show()  
     

    3、设置标签

    [python] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import matplotlib.pyplot as plt  
    3.   
    4. name_list = ['Monday','Tuesday','Friday','Sunday']  
    5. num_list = [1.5,0.6,7.8,6]  
    6. plt.bar(range(len(num_list)), num_list,color='rgb',tick_label=name_list)  
    7. plt.show()  

    4、堆叠柱状图

    [python] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import matplotlib.pyplot as plt  
    3.   
    4. name_list = ['Monday','Tuesday','Friday','Sunday']  
    5. num_list = [1.5,0.6,7.8,6]  
    6. num_list1 = [1,2,3,1]  
    7. plt.bar(range(len(num_list)), num_list, label='boy',fc = 'y')  
    8. plt.bar(range(len(num_list)), num_list1, bottom=num_list, label='girl',tick_label = name_list,fc = 'r')  
    9. plt.legend()  
    10. plt.show()  

    5、并列柱状图

    [python] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import matplotlib.pyplot as plt  
    3.   
    4. name_list = ['Monday','Tuesday','Friday','Sunday']  
    5. num_list = [1.5,0.6,7.8,6]  
    6. num_list1 = [1,2,3,1]  
    7. x =list(range(len(num_list)))  
    8. total_width, n = 0.8, 2  
    9. width = total_width / n  
    10.   
    11. plt.bar(x, num_list, width=width, label='boy',fc = 'y')  
    12. for i in range(len(x)):  
    13.     x[i] = x[i] + width  
    14. plt.bar(x, num_list1, width=width, label='girl',tick_label = name_list,fc = 'r')  
    15. plt.legend()  
    16. plt.show()  
     

    6、条形柱状图

    [python] view plain copy
     
    1. # -*- coding: utf-8 -*-  
    2. import matplotlib.pyplot as plt  
    3.   
    4. name_list = ['Monday','Tuesday','Friday','Sunday']  
    5. num_list = [1.5,0.6,7.8,6]  
    6. plt.barh(range(len(num_list)), num_list,tick_label = name_list)  
    7. plt.show()  
  • 相关阅读:
    Less资源汇总
    Less函数说明
    Less使用说明
    Less 简介
    tomcat-users.xml 配置
    java project打包生成jar包(通用)
    5分钟理解String的'+'的性能及原理
    字符串"+"操作的原理
    Shell脚本实现对文件编辑
    mysql关于聚集索引、非聚集索引的总结
  • 原文地址:https://www.cnblogs.com/decode1234/p/8535638.html
Copyright © 2011-2022 走看看