04-matplotlib-柱形图

1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 # 柱形图 5 # 例一 6 N =5 7 y = [15,28,10,30,25] 8 index = np.arange(N) 9 p = plt.bar(index,height=y) 10 plt.show() 11 12 # 例2 13 p1 = plt.bar(0,bottom=index,width=y,height=0.5,align='edge',color='red',orientation='horizontal') 14 plt.show() 15 16 p2 = plt.barh(index,width= y , align='edge',color ='green',height=0.5) 17 plt.show() 18 19 # 例3 20 sales_BJ = [52,55,63,53] 21 sales_SH = [44,66,55,41] 22 23 index = np.arange(4) 24 bar_width = 0.3 25 26 # 竖着显示 27 plt.bar(index,sales_BJ,bar_width) 28 plt.bar(index+bar_width,sales_SH,bar_width,color='r') 29 plt.show() 30 31 # 横着显示 32 plt.barh(index,sales_BJ,bar_width) 33 plt.barh(index+bar_width,sales_SH,bar_width,color='r') 34 plt.show() 35 36 # 层叠图 37 plt.bar(index,sales_BJ,bar_width) 38 plt.bar(index,sales_SH,bar_width,color='r',bottom=sales_BJ) 39 plt.show() 40 41 # 练习 42 ''' 43 生成两组大小为5的数据; 44 画出两组数据 水平的条形图; 45 采用并列,层叠两种方式; 46 47 ''' 48 49 N =5 50 n1 = np.random.randint(1,100,N) 51 n2 = np.random.randint(1,100,N) 52 53 index = np.arange(N) 54 bar_width = 0.3 55 56 # 并列显示 57 plt.bar(index,n1,bar_width) 58 plt.bar(index+bar_width,n2,bar_width,color='r') 59 plt.show() 60 61 # 层叠显示 62 plt.bar(index,n1,bar_width) 63 plt.bar(index,n2,bar_width,color='r',bottom=n1) 64 plt.show()

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zywwzz.html