Python繪制組合圖的示例
繪制組合圖:
組合圖就是將多個(gè)形狀,組合到⼀個(gè)圖形中,主要作⽤是節(jié)約作圖的空間,節(jié)省讀者的時(shí)間,從⽽提⾼信息傳達(dá)的效率。
import pandas as pdimport numpy as npimport matplotlib.pyplot as pltdef plot_combination1(): sale = pd.read_excel(’./data/每月目標(biāo)銷(xiāo)售額和實(shí)際銷(xiāo)售額.xlsx’,header=0,index_col=0) # 設(shè)置正常顯示中文標(biāo)簽 plt.rcParams[’font.sans-serif’] = [’SimHei’] # 正常顯示負(fù)號(hào) plt.rcParams[’axes.unicode_minus’] = False # 設(shè)置字體大小 plt.rcParams.update({’font.size’:16}) # 提取數(shù)據(jù) x = np.arange(12)+1 y1 = sale.目標(biāo)銷(xiāo)售額 y2 = sale.實(shí)際銷(xiāo)售額 # 計(jì)算目標(biāo)完成率 y3 = y2/y1 # float # print(y3) 1月 1.120000 2月 0.887500 3月 1.118182 4月 1.150000 ''' 第一種方式:是⽤兩個(gè)不同顏⾊的柱⼦,分別展示每個(gè)⽉的實(shí)際銷(xiāo)售額和⽬標(biāo)銷(xiāo)售額, ⽤折線圖展示⽬標(biāo)完成率。 左邊的主坐標(biāo)軸是柱形圖對(duì)應(yīng)的數(shù)據(jù),右邊的次坐標(biāo)軸是折線圖對(duì)應(yīng)的 數(shù)據(jù),下邊的橫坐標(biāo)軸表示細(xì)分的維度,⽐如時(shí)間、地區(qū)、渠道等。 ''' plt.figure(figsize=(16,8)) plt.subplot(111) # 柱形寬度 bar_width = 0.35 # 在主坐標(biāo)軸繪制柱形圖 plt.bar(x,y1,bar_width,label=’目標(biāo)銷(xiāo)售額’) plt.bar(x+bar_width,y2,bar_width,label=’實(shí)際銷(xiāo)售額’) # 設(shè)置坐標(biāo)軸的取值范圍,避免柱子過(guò)高而與圖例重疊 plt.ylim(0,max(y1.max(),y2.max())*1.2) # 設(shè)置圖例 plt.legend(loc=’upper left’) # 設(shè)置橫坐標(biāo)的標(biāo)簽 plt.xticks(x) # plt.set_xticklabels(sale.index) # 在次坐標(biāo)軸上繪制折線圖 plt.twinx() # ls:線的類(lèi)型,lw:寬度,o:在頂點(diǎn)處實(shí)心圈 plt.plot(x,y3,ls=’-’,lw=2,color=’r’,marker=’o’,label=’目標(biāo)完成率’) # 設(shè)置次坐標(biāo)軸的取值范圍,避免折線圖波動(dòng)過(guò)大 plt.ylim(0,1.35) # 設(shè)置圖例 plt.legend() # 定義顯示百分號(hào)的函數(shù) def to_percent(number, position=0): return ’%.f’ % (number * 100) + ’%’ # 次坐標(biāo)軸的標(biāo)簽顯示百分號(hào) FuncFormatter:自定義格式函數(shù)包 from matplotlib.ticker import FuncFormatter plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent)) # 設(shè)置標(biāo)題 plt.title(’n每月銷(xiāo)售目標(biāo)達(dá)成情況n’,fontsize=36,loc=’center’,color = ’k’) plt.show()def plot_combination2(): ''' 第二種方式:是⽤兩條不同顏⾊的折線,分別展示每個(gè)⽉的實(shí)際銷(xiāo)售額和⽬標(biāo)銷(xiāo)售額,再⽤兩種不同顏 ⾊的柱形圖展示實(shí)際與⽬標(biāo)的差額,綠⾊代表完成⽬標(biāo),紅⾊代表沒(méi)有完成⽬標(biāo), 這種組合圖不需要⽤到兩個(gè)縱坐標(biāo)軸, ''' import pandas as pd import numpy as np import matplotlib.pyplot as plt # 設(shè)置正常顯示中⽂標(biāo)簽 plt.rcParams[’font.sans-serif’] = [’SimHei’] # 正常顯示負(fù)號(hào) plt.rcParams[’axes.unicode_minus’] = False # 設(shè)置字體⼤⼩ plt.rcParams.update({’font.size’: 16}) # 從 Excel ⽂件中讀取數(shù)據(jù),第⼀列設(shè)置為索引 sale = pd.read_excel(’./data/每月目標(biāo)銷(xiāo)售額和實(shí)際銷(xiāo)售額.xlsx’, index_col=0) # 提取數(shù)據(jù) # print(’index’) x = sale.index # Index([’1月’, ’2月’, ’3月’, ’4月’, ’5月’, ’6月’, ’7月’, ’8月’, ’9月’, ’10月’, ’11月’, ’12月’], dtype=’object’, name=’month’) # print(x) y1 = sale.目標(biāo)銷(xiāo)售額 y2 = sale.實(shí)際銷(xiāo)售額 # 計(jì)算差額 y3 = y2 - y1 # 繪制折線圖 plt.figure(figsize=(16, 8)) plt.subplot(111) plt.plot(x, y1, ls=’-’, lw=2, label=’目標(biāo)銷(xiāo)售額’) plt.plot(x, y2, ls=’--’, lw=2, label=’實(shí)際銷(xiāo)售額’) # ⽤列表推導(dǎo)式定義柱⼦的顏⾊,綠⾊代表完成⽬標(biāo), 紅⾊代表沒(méi)有完成⽬標(biāo) color = [’g’ if i > 0 else ’#dc5034’ for i in y3] # 繪制柱形圖 plt.bar(x, y3, color=color, label=’差額’) # 設(shè)置圖例 plt.legend(loc=’upper left’) # 設(shè)置標(biāo)題 title = ’n每月銷(xiāo)售目標(biāo)達(dá)成情況n’ plt.title(title, fontsize=36, loc=’center’, color=’k’) plt.show()if __name__ == ’__main__’: plot_combination1() plot_combination2()
繪制結(jié)果:
第一種
第二種:
參考書(shū)目:
數(shù)據(jù)化分析 Python 實(shí)戰(zhàn) - 林驥
以上就是Python繪制組合圖的示例的詳細(xì)內(nèi)容,更多關(guān)于Python繪制組合圖的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 如何使用Python的Pandas庫(kù)繪制折線圖2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)財(cái)務(wù)記賬管理系統(tǒng)3. php中yii框架實(shí)例用法4. HTML實(shí)現(xiàn)title 屬性換行小技巧5. 父div高度不能自適應(yīng)子div高度的解決方案6. ASP替換、保存遠(yuǎn)程圖片實(shí)現(xiàn)代碼7. PHP中Session會(huì)話的使用和分析8. SSM框架整合之Spring+SpringMVC+MyBatis實(shí)踐步驟9. 三道java新手入門(mén)面試題,通往自由的道路--鎖+Volatile10. 網(wǎng)頁(yè)中img圖片使用css實(shí)現(xiàn)等比例自動(dòng)縮放不變形(代碼已測(cè)試)
