手把手帶你了解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í)針排列,為T(mén)rue時(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)圖
本篇文章就到這里了,希望能給你帶來(lái)幫助,也希望您能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!
相關(guān)文章:
1. 利用CSS制作3D動(dòng)畫(huà)2. ThinkPHP5 通過(guò)ajax插入圖片并實(shí)時(shí)顯示(完整代碼)3. Python的Tqdm模塊實(shí)現(xiàn)進(jìn)度條配置4. 刪除docker里建立容器的操作方法5. WML語(yǔ)言的基本情況6. react axios 跨域訪問(wèn)一個(gè)或多個(gè)域名問(wèn)題7. Properties 持久的屬性集的實(shí)例詳解8. Python 多線程之threading 模塊的使用9. .NET6打包部署到Windows Service的全過(guò)程10. CSS代碼檢查工具stylelint的使用方法詳解
