手把手帶你了解Python數(shù)據(jù)分析--matplotlib
柱形圖
bar()函數(shù)繪制柱形圖
import matplotlib.pyplot as pl x = [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è)置每根柱子的寬度和顏色
有中文時要添加
pl.rcParams['font.sans-serif'] = ['FangSong']
有負號時要添加
pl.rcParams['axes.unicode_minus'] = False
import matplotlib.pyplot as pl pl.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 pl pl.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 pl pl.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è)置折線的粗細(單位為“點”)
參數(shù)linestyle用于設(shè)置折線的線型

marker= '*'表示設(shè)置數(shù)據(jù)標記的樣式為五角星
markersize=10表示設(shè)置數(shù)據(jù)標記的大小為10點

餅圖和圓環(huán)圖
pie()函數(shù)可繪制餅圖
import matplotlib.pyplot as pl pl.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è)置每一個餅圖塊的標簽
參數(shù)labeldistance用于設(shè)置每一個餅圖塊的標簽與中心的距離
參數(shù)autopct用于設(shè)置百分比數(shù)值的格式
參數(shù)pctdistance用于設(shè)置百分比數(shù)值與中心的距離
分離餅圖塊
import matplotlib.pyplot as pl pl.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è)置每一個餅圖塊與圓心的距離,其值通常是一個列表,列表的元素個數(shù)與餅圖塊的數(shù)量相同。這里設(shè)置為[0, 0, 0, 0, 0, 0.3],第5個元素為0.3,其他元素均為0,表示將第5個餅圖塊分離。
參數(shù)startangle用于設(shè)置第1個餅圖塊的初始角度
參數(shù)counterclock用于設(shè)置各個餅圖塊是逆時針排列還是順時針排列,為False時表示順時針排列,為True時表示逆時針排列。

圓環(huán)圖
import matplotlib.pyplot as pl
pl.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
邊框粗細為2
邊框顏色為白色。
將餅圖塊的環(huán)寬占比設(shè)置為小于1的數(shù)(這里為0.3)就能繪制出圓環(huán)圖

總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注本站的更多內(nèi)容!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信