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)銷售額和實(shí)際銷售額.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)銷售額 y2 = sale.實(shí)際銷售額 # 計(jì)算目標(biāo)完成率 y3 = y2/y1 # float # print(y3) 1月 1.120000 2月 0.887500 3月 1.118182 4月 1.150000 ''' 第一種方式:是⽤兩個(gè)不同顏⾊的柱⼦,分別展示每個(gè)⽉的實(shí)際銷售額和⽬標(biāo)銷售額, ⽤折線圖展示⽬標(biāo)完成率。 左邊的主坐標(biāo)軸是柱形圖對應(yīng)的數(shù)據(jù),右邊的次坐標(biāo)軸是折線圖對應(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)銷售額’) plt.bar(x+bar_width,y2,bar_width,label=’實(shí)際銷售額’) # 設(shè)置坐標(biāo)軸的取值范圍,避免柱子過高而與圖例重疊 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:線的類型,lw:寬度,o:在頂點(diǎn)處實(shí)心圈 plt.plot(x,y3,ls=’-’,lw=2,color=’r’,marker=’o’,label=’目標(biāo)完成率’) # 設(shè)置次坐標(biāo)軸的取值范圍,避免折線圖波動(dòng)過大 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每月銷售目標(biāo)達(dá)成情況n’,fontsize=36,loc=’center’,color = ’k’) plt.show()def plot_combination2(): ''' 第二種方式:是⽤兩條不同顏⾊的折線,分別展示每個(gè)⽉的實(shí)際銷售額和⽬標(biāo)銷售額,再⽤兩種不同顏 ⾊的柱形圖展示實(shí)際與⽬標(biāo)的差額,綠⾊代表完成⽬標(biāo),紅⾊代表沒有完成⽬標(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)銷售額和實(shí)際銷售額.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)銷售額 y2 = sale.實(shí)際銷售額 # 計(jì)算差額 y3 = y2 - y1 # 繪制折線圖 plt.figure(figsize=(16, 8)) plt.subplot(111) plt.plot(x, y1, ls=’-’, lw=2, label=’目標(biāo)銷售額’) plt.plot(x, y2, ls=’--’, lw=2, label=’實(shí)際銷售額’) # ⽤列表推導(dǎo)式定義柱⼦的顏⾊,綠⾊代表完成⽬標(biāo), 紅⾊代表沒有完成⽬標(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每月銷售目標(biāo)達(dá)成情況n’ plt.title(title, fontsize=36, loc=’center’, color=’k’) plt.show()if __name__ == ’__main__’: plot_combination1() plot_combination2()
繪制結(jié)果:
第一種
第二種:
參考書目:
數(shù)據(jù)化分析 Python 實(shí)戰(zhàn) - 林驥
以上就是Python繪制組合圖的示例的詳細(xì)內(nèi)容,更多關(guān)于Python繪制組合圖的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 小區(qū)后臺(tái)管理系統(tǒng)項(xiàng)目前端html頁面模板實(shí)現(xiàn)示例2. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)3. asp讀取xml文件和記數(shù)4. 前端從瀏覽器的渲染到性能優(yōu)化5. ASP.NET MVC遍歷驗(yàn)證ModelState的錯(cuò)誤信息6. ASP新手必備的基礎(chǔ)知識(shí)7. PHP中file_get_contents設(shè)置header請求頭,curl傳輸選項(xiàng)參數(shù)詳解說明8. asp知識(shí)整理筆記2(問答模式)9. python—sys模塊之獲取參數(shù)的操作10. python opencv通過按鍵采集圖片源碼
