Python+matplotlib實(shí)現(xiàn)餅圖的繪制
一、整理數(shù)據(jù)
關(guān)于cnboo1.xlsx,我放在我的碼云里,需要的朋友自行下載:cnboo1.xlsx
films=['穿過寒冬擁抱你','反貪風(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問:為圖表添加漫畫效果
三、爆炸效果
# 爆炸效果 餅圖脫離 from matplotlib import pyplot as plt? explo=[0.3,0,0,0,0,0] # 控制爆炸效果,通過更改參數(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()
四、陰影效果
# 添加陰影效果 # 爆炸效果 餅圖脫離 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()
五、為餅圖加上百分比
# 添加陰影效果 # 爆炸效果 餅圖脫離 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()
六、讓餅圖旋轉(zhuǎ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()
七、為餅圖添加邊緣線
# 為餅圖添加邊緣線 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()
但是我自己感覺并不是非常明顯
八、為餅圖數(shù)據(jù)分組
# 將數(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)餅圖的繪制的詳細(xì)內(nèi)容,更多關(guān)于Python matplotlib餅圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Python數(shù)據(jù)分析之?Matplotlib?餅圖繪制
- Python+matplotlib繪制餅圖和堆疊圖
- Python利用matplotlib實(shí)現(xiàn)餅圖繪制
- python matplotlib模塊基本圖形繪制方法小結(jié)【直線,曲線,直方圖,餅圖等】
- python通過matplotlib生成復(fù)合餅圖
- python使用Matplotlib畫餅圖
- Python通過matplotlib畫雙層餅圖及環(huán)形圖簡(jiǎn)單示例
- python利用matplotlib庫(kù)繪制餅圖的方法示例
- Python 如何利用pandas和matplotlib繪制餅圖
相關(guān)文章
pandas.concat實(shí)現(xiàn)DataFrame豎著拼接、橫著拼接方式
這篇文章主要介紹了pandas.concat實(shí)現(xiàn)DataFrame豎著拼接、橫著拼接方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10python內(nèi)置進(jìn)制轉(zhuǎn)換函數(shù)的操作
這篇文章主要介紹了python內(nèi)置進(jìn)制轉(zhuǎn)換函數(shù)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Python實(shí)現(xiàn)端口復(fù)用實(shí)例代碼
這篇文章主要介紹了Python實(shí)現(xiàn)端口復(fù)用實(shí)例代碼,需要的朋友可以參考下2014-07-07python實(shí)現(xiàn)每次處理一個(gè)字符的三種方法
這篇文章主要介紹了python實(shí)現(xiàn)每次處理一個(gè)字符的三種方法,是非常實(shí)用的字符串操作技巧,需要的朋友可以參考下2014-10-10Python中的內(nèi)置函數(shù)isdigit()
這篇文章主要介紹了Python中的內(nèi)置函數(shù)isdigit(),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法
今天小編就為大家分享一篇利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07