溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

發(fā)布時(shí)間:2022-03-08 14:43:51 來(lái)源:億速云 閱讀:123 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

一、整理數(shù)據(jù)

關(guān)于cnboo1.xlsx,我放在我的碼云里,需要的朋友自行下載:cnboo1.xlsx

films=['穿過(guò)寒冬擁抱你','反貪風(fēng)暴5:最終章','李茂扮太子','誤殺2','以年為單位的戀愛','黑客帝國(guó):矩陣重啟','雄獅少年','魔法滿屋','汪汪隊(duì)立大功大電影','愛情神話']
regions=['中國(guó)','英國(guó)','澳大利亞','美國(guó)','美國(guó)','中國(guó)','英國(guó)','澳大利亞','美國(guó)','美國(guó)']
bos=['61,181','44,303','42,439','22,984','13,979','61,181','44,303','41,439','20,984','19,979']
persons=['31','23','56','17','9','31','23','56','17','9']
prices=['51','43','56','57','49','51','43','56','57','49']
showdate=['2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05','2022-12-03','2022-12-05','2022-12-01','2022-12-02','2022-11-05']
ftypes=['劇情','動(dòng)作','喜劇','劇情','劇情','愛情','動(dòng)作','動(dòng)畫','動(dòng)畫','動(dòng)畫']
points=['8.1','9.0','7.9','6.7','3.8','8.1','9.0','7.9','6.7','3.8']
filmdescript={
    'ftypes':ftypes,
    'bos':bos,
    'prices':prices,
    'persons':persons,
    'regions':regions,
    'showdate':showdate,
    'points':points
}
import numpy as np
import pandas as pd
cnbo2021top5=pd.DataFrame(filmdescript,index=films)
cnbo2021top5[['prices','persons']]=cnbo2021top5[['prices','persons']].astype(int)
cnbo2021top5['bos']=cnbo2021top5['bos'].str.replace(',','').astype(int)
cnbo2021top5['showdate']=cnbo2021top5['showdate'].astype('datetime64')
cnbo2021top5['points']=cnbo2021top5['points'].apply(lambda x:float(x) if x!='' else 0)
import pandas as pd 
cnbodf=pd.read_excel('cnboo1.xlsx')
cnbodfsort=cnbodf.sort_values(by=['BO'],ascending=False)
cnbodfsort.index=cnbodfsort.TYPE
bo=cnbo2021top5.bos.sort_values()
def mkpoints(x,y):
    return len(str(x))*(y/25)-3

cnbodfsort['points']=cnbodfsort.apply(lambda x:mkpoints(x.BO,x.PERSONS),axis=1)
cnbodfsort['type1']=cnbodfsort['TYPE'].apply(lambda x:x.split("/")[0])
cnbodfgb=cnbodfsort.groupby(["type1"])["ID","BO","PRICE","PERSONS","points"].mean()
cnbodfgbsort=cnbodfgb.sort_values("BO",ascending=False)

二、創(chuàng)建餅圖

from matplotlib import pyplot as plt 
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中國(guó)票房2021TOP9") 
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index)
plt.show()

這里涉及到簡(jiǎn)歷的漫畫效果:詳情請(qǐng)?jiān)L問(wèn):為圖表添加漫畫效果

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

三、爆炸效果

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

# 爆炸效果 餅圖脫離

from matplotlib import pyplot as plt 
explo=[0.3,0,0,0,0,0] # 控制爆炸效果,通過(guò)更改參數(shù)控制距離的長(zhǎng)短
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中國(guó)票房2021TOP9") 
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo)
plt.show()

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

四、陰影效果

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

# 添加陰影效果
# 爆炸效果 餅圖脫離

from matplotlib import pyplot as plt 
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中國(guó)票房2021TOP9") 
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True)
plt.show()

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

五、為餅圖加上百分比

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

# 添加陰影效果
# 爆炸效果 餅圖脫離

from matplotlib import pyplot as plt 
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中國(guó)票房2021TOP9") 
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=0,autopct='%1.2f%%')
plt.show()

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

六、讓餅圖旋轉(zhuǎn)不同的角度

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

# 餅圖旋轉(zhuǎn)
from matplotlib import pyplot as plt 
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中國(guó)票房2021TOP9") 
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%')
plt.show()

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

七、為餅圖添加邊緣線

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

# 為餅圖添加邊緣線
from matplotlib import pyplot as plt 
explo=[0.3,0,0,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中國(guó)票房2021TOP9") 
plt.pie(cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%',wedgeprops={"edgecolor":"black"})
plt.show()

但是我自己感覺(jué)并不是非常明顯

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

八、為餅圖數(shù)據(jù)分組

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

# 將數(shù)據(jù)按照票房分類
labels=['>20000','15000-20000','10000-15000','<10000']
c1=cnbodfsort.loc[cnbodfsort.BO>=20000].count()[0]
c2=cnbodfsort.loc[(cnbodfsort.BO>=15000) & (cnbodfsort.BO<20000)].count()[0]
c3=cnbodfsort.loc[(cnbodfsort.BO<15000) & (cnbodfsort.BO>=10000)].count()[0]
c4=cnbodfsort.loc[cnbodfsort.BO<10000].count()[0]
cnbohints=[c1,c2,c3,c4]
from matplotlib import pyplot as plt 
explo=[0.3,0,0,0] # 控制爆炸效果
plt.style.use('seaborn')
plt.figure(figsize=(15,9))
plt.rcParams.update({'font.family': "Microsoft YaHei"})
plt.title("中國(guó)票房2021TOP9") 
plt.pie(cnbohints,labels=labels,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%',wedgeprops={"edgecolor":"black"})
plt.show()

Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制

以上是“Python+matplotlib如何實(shí)現(xiàn)餅圖的繪制”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI