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

在將來面前,咱們永遠都是孩子。不斷思考,不斷學習,才能讓咱們走的更遠。dom

我的主頁:https://www.oceaneyes.cn/學習

我的學習博客:http://oceaneyes.top/spa

CSDN:https://blog.csdn.net/qq_16123129 .net

 長按二維碼關注,一塊兒交流學習~~~code

相關文章
相關標籤/搜索