手把手帶你了解Python數(shù)據(jù)分析--matplotlib
bar()函數(shù)繪制柱形圖
import matplotlib.pyplot as plx = [1,2,3,4,5,6,7]y = [15,69,85,12,36,95,11]pl.bar(x,y)pl.show()
bar()函數(shù)的參數(shù)width和color設(shè)置每根柱子的寬度和顏色有中文時(shí)要添加pl.rcParams[’font.sans-serif’] = [’FangSong’]有負(fù)號(hào)時(shí)要添加pl.rcParams[’axes.unicode_minus’] = False
import matplotlib.pyplot as plpl.rcParams[’font.sans-serif’] = [’FangSong’]x = [’一’,’二’,’三’,’四’,’五’]y = [25,63,98,20,15]pl.bar(x,y,width=0.5,color=’red’)pl.show()
barh()函數(shù)可繪制條形圖
參數(shù)height設(shè)置條形的高度
import matplotlib.pyplot as plpl.rcParams[’font.sans-serif’] = [’FangSong’]x = [’一’,’二’,’三’,’四’,’五’]y = [25,63,98,20,15]pl.barh(x,y,height=0.5,color=’red’)pl.show()
plot()函數(shù)可繪制折線圖
import matplotlib.pyplot as plpl.rcParams[’font.sans-serif’] = [’FangSong’]x = [’一’,’二’,’三’,’四’,’五’]y = [25,63,98,20,15]pl.plot(x,y,linewidth=2,linestyle=’-’,color=’red’,marker=’*’,markersize=10)pl.show()
參數(shù)linewidth用于設(shè)置折線的粗細(xì)(單位為“點(diǎn)”)參數(shù)linestyle用于設(shè)置折線的線型
marker= ’*’表示設(shè)置數(shù)據(jù)標(biāo)記的樣式為五角星markersize=10表示設(shè)置數(shù)據(jù)標(biāo)記的大小為10點(diǎn)
pie()函數(shù)可繪制餅圖
import matplotlib.pyplot as plpl.rcParams[’font.sans-serif’] = [’FangSong’]x = [’一’,’二’,’三’,’四’,’五’]y = [25,63,98,20,15]pl.pie(y,labels=x,labeldistance=1,autopct=’%.2f%%’,pctdistance=1.2)pl.show()
參數(shù)labels用于設(shè)置每一個(gè)餅圖塊的標(biāo)簽參數(shù)labeldistance用于設(shè)置每一個(gè)餅圖塊的標(biāo)簽與中心的距離參數(shù)autopct用于設(shè)置百分比數(shù)值的格式參數(shù)pctdistance用于設(shè)置百分比數(shù)值與中心的距離
分離餅圖塊import matplotlib.pyplot as plpl.rcParams[’font.sans-serif’] = [’FangSong’]x = [’一’,’二’,’三’,’四’,’五’]y = [25,63,98,20,15]pl.pie(y,labels=x,labeldistance=1,autopct=’%.2f%%’,pctdistance=1.2,explode=[0,0,0,0,0.3],startangle=90,counterclock=False)pl.show()
參數(shù)explode用于設(shè)置每一個(gè)餅圖塊與圓心的距離,其值通常是一個(gè)列表,列表的元素個(gè)數(shù)與餅圖塊的數(shù)量相同。這里設(shè)置為[0, 0, 0, 0, 0, 0.3],第5個(gè)元素為0.3,其他元素均為0,表示將第5個(gè)餅圖塊分離。參數(shù)startangle用于設(shè)置第1個(gè)餅圖塊的初始角度參數(shù)counterclock用于設(shè)置各個(gè)餅圖塊是逆時(shí)針排列還是順時(shí)針排列,為False時(shí)表示順時(shí)針排列,為True時(shí)表示逆時(shí)針排列。
import matplotlib.pyplot as plpl.rcParams[’font.sans-serif’] = [’FangSong’]x = [’一’,’二’,’三’,’四’,’五’]y = [25,63,98,20,15]pl.pie(y,labels=x,labeldistance=1,autopct=’%.2f%%’,pctdistance=1.2,explode=[0,0,0,0,0.3],startangle=90,counterclock=False, wedgeprops={’width’:0.5,’linewidth’:2,’edgecolor’:’white’})pl.show()
wedgeprops={‘width’: 0.5, ‘linewidth’:2, ‘edgecolor’: ‘white’}表示設(shè)置餅圖塊的環(huán)寬(圓環(huán)的外圓半徑減去內(nèi)圓半徑)占外圓半徑的比例為0.5邊框粗細(xì)為2邊框顏色為白色。將餅圖塊的環(huán)寬占比設(shè)置為小于1的數(shù)(這里為0.3)就能繪制出圓環(huán)圖
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. python全棧開發(fā)語法總結(jié)3. Python調(diào)用接口合并Excel表代碼實(shí)例4. 如何在Python項(xiàng)目中引入日志5. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖6. python b站視頻下載的五種版本7. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)8. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效9. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析10. Python快速將ppt制作成配音視頻課件的操作方法
