Python画柱状图

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.82  
  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()  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值