1 """
2 繪制組合圖: 3 組合圖就是將多個形狀,組合到⼀個圖形中,主要作⽤是節約作圖的空間,節省讀者的時間,從⽽提⾼ 4 信息傳達的效率。 5 """
6 import pandas as pd 7 import numpy as np 8 import matplotlib.pyplot as plt 9 def plot_combination1(): 10 sale = pd.read_excel('./data/每月目標銷售額和實際銷售額.xlsx',header=0,index_col=0) 11 # 設置正常顯示中文標簽
12 plt.rcParams['font.sans-serif'] = ['SimHei'] 13 # 正常顯示負號
14 plt.rcParams['axes.unicode_minus'] = False 15 # 設置字體大小
16 plt.rcParams.update({'font.size':16}) 17
18 # 提取數據
19 x = np.arange(12)+1
20 y1 = sale.目標銷售額 21 y2 = sale.實際銷售額 22
23 # 計算目標完成率
24 y3 = y2/y1 # float
25 # print(y3) 1月 1.120000 2月 0.887500 3月 1.118182 4月 1.150000
26 """
27 第一種方式:是⽤兩個不同顏⾊的柱⼦,分別展示每個⽉的實際銷售額和⽬標銷售額, 28 ⽤折線圖展示⽬標完成率。 29 左邊的主坐標軸是柱形圖對應的數據,右邊的次坐標軸是折線圖對應的 30 數據,下邊的橫坐標軸表示細分的維度,⽐如時間、地區、渠道等。 31 """
32 plt.figure(figsize=(16,8)) 33 plt.subplot(111) 34
35 # 柱形寬度
36 bar_width = 0.35
37
38 # 在主坐標軸繪制柱形圖
39 plt.bar(x,y1,bar_width,label='目標銷售額') 40 plt.bar(x+bar_width,y2,bar_width,label='實際銷售額') 41
42 # 設置坐標軸的取值范圍,避免柱子過高而與圖例重疊
43 plt.ylim(0,max(y1.max(),y2.max())*1.2) 44
45 # 設置圖例
46 plt.legend(loc='upper left') 47
48 # 設置橫坐標的標簽
49 plt.xticks(x) 50 # plt.set_xticklabels(sale.index)
51
52 # 在次坐標軸上繪制折線圖
53 plt.twinx() 54 # ls:線的類型,lw:寬度,o:在頂點處實心圈
55 plt.plot(x,y3,ls='-',lw=2,color='r',marker='o',label='目標完成率') 56
57 # 設置次坐標軸的取值范圍,避免折線圖波動過大
58 plt.ylim(0,1.35) 59
60 # 設置圖例
61 plt.legend() 62
63 # 定義顯示百分號的函數
64 def to_percent(number, position=0): 65 return '%.f' % (number * 100) + '%'
66
67 # 次坐標軸的標簽顯示百分號 FuncFormatter:自定義格式函數包
68 from matplotlib.ticker import FuncFormatter 69 plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent)) 70
71 # 設置標題
72 plt.title('\n每月銷售目標達成情況\n',fontsize=36,loc='center',color = 'k') 73 plt.show() 74
75
76
77 def plot_combination2(): 78 """
79 第二種方式:是⽤兩條不同顏⾊的折線,分別展示每個⽉的實際銷售額和⽬標銷售額,再⽤兩種不同顏 80 ⾊的柱形圖展示實際與⽬標的差額,綠⾊代表完成⽬標,紅⾊代表沒有完成⽬標, 81 這種組合圖不需要⽤到兩個縱坐標軸, 82 """
83 import pandas as pd 84 import numpy as np 85 import matplotlib.pyplot as plt 86
87 # 設置正常顯示中⽂標簽
88 plt.rcParams['font.sans-serif'] = ['SimHei'] 89
90 # 正常顯示負號
91 plt.rcParams['axes.unicode_minus'] = False 92
93 # 設置字體⼤⼩
94 plt.rcParams.update({'font.size': 16}) 95
96 # 從 Excel ⽂件中讀取數據,第⼀列設置為索引
97 sale = pd.read_excel('./data/每月目標銷售額和實際銷售額.xlsx', index_col=0) 98 # 提取數據
99 # print('index')
100 x = sale.index # Index(['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月', '9月', '10月', '11月', '12月'], dtype='object', name='month')
101 # print(x)
102 y1 = sale.目標銷售額 103 y2 = sale.實際銷售額 104 # 計算差額
105 y3 = y2 - y1 106 # 繪制折線圖
107 plt.figure(figsize=(16, 8)) 108 plt.subplot(111) 109 plt.plot(x, y1, ls='-', lw=2, label='目標銷售額') 110 plt.plot(x, y2, ls='--', lw=2, label='實際銷售額') 111 # ⽤列表推導式定義柱⼦的顏⾊,綠⾊代表完成⽬標, 紅⾊代表沒有完成⽬標
112 color = ['g' if i > 0 else '#dc5034' for i in y3] 113
114 # 繪制柱形圖
115 plt.bar(x, y3, color=color, label='差額') 116 # 設置圖例
117 plt.legend(loc='upper left') 118 # 設置標題
119 title = '\n每月銷售目標達成情況\n'
120 plt.title(title, fontsize=36, loc='center', color='k') 121 plt.show() 122
123 if __name__ == '__main__': 124 plot_combination1() 125 plot_combination2()
結果:
第一種情況:
第二種:
參考書目:
數據化分析 Python 實戰 - 林驥