How to realize the drawing of pie chart by Python+matplotlib
This article mainly introduces Python+matplotlib how to achieve the pie chart drawing, the article introduces in great detail, has a certain reference value, interested friends must read it!
I. collate the data
About cnboo1.xlsx, I put it in my cloud, and friends who need it can download it by themselves: cnboo1.xlsx
Films= ['hug you through the cold winter', 'Anti-Corruption Storm 5: final Chapter','Li Mao pretends to be the Crown Prince', 'mistakenly killed 2You Magic', 'Matrix restart', 'male Lion Boys', 'House full of Magic', 'Wang team made Great Movie', 'Love Myth'] regions= ['China', 'Britain', 'Australia', 'USA', 'China' 'Britain', 'Australia', 'United States', 'United States'] bos= ['61181', '44303', '42439', '22984', '13979', '61181', '44303', '41439',' 20984', '19979'] persons= ['31',' 271, '23',' 56', '17', 9',' 31', '23',' 56', '17',' 91'] prices= ['51',' 43', '56',' 57', '57',' 51' Showdate= [2022-12-03] ftypes= [2022-12-01, 2022-12-01, 2022-12-01, 2022-12-05, 2022-12-03, 2022-12-05, 2022-12-03, 2022-12-03, 2022-12-05, 2022-12-05, 2022-12-03, 2022-12-05, 2022-12-05, 2022-12-05, 2022-12-05, 2022-12-05] Action, Animation] points= ['8.1' bos':bos, 'prices':prices,' persons':persons, 'regions':regions,' showdate':showdate 'points':points} import numpy as npimport pandas as pdcnbo2021top5=pd.DataFrame (filmdescript,index=films) cnbo2021top5 [[' prices','persons']] = cnbo2021top5 [['prices','persons']] .astype (int) cnbo2021top5 [' bos'] = cnbo2021top5 ['bos'] .str.replace (',' '') .Aastype (int) cnbo2021top5 ['showdate'] = cnbo2021top5 [' showdate'] .astype ('datetime64') cnbo2021top5 [' points'] = cnbo2021top5 ['points'] .apply (lambda x:float (x) if x thanks thanks' else 0) import pandas as pd cnbodf=pd.read_excel ('cnboo1.xlsx') cnbodfsort=cnbodf.sort_values (by= [' BO'], ascending=False) cnbodfsort.index=cnbodfsort.TYPEbo=cnbo2021top5.bos.sort_values () def mkpoints (x) Y): return len (str (x)) * (yUnip 25)-3cnbodfsort ['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) II. Create pie chart from matplotlib import pyplot as plt plt.style.use ('seaborn') plt.figure (figsize= (15jue 9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.pie (cnbodfgbsort.BO,labels=cnbodfgbsort.index) plt.show ()
Here is the cartoon effect of the resume: for more information, please visit: add cartoon effect to the chart.
Third, explosion effect
# explosion effect pie chart is separated from from matplotlib import pyplot as plt explo= [0.3 seaborn' 0] # Control the explosion effect by changing the parameters to control the length of the distance plt.style.use ('plt.rcParams.update) plt.figure (figsize= (15) 9) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.pie (cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo) plt.show ()
Fourth, shadow effect
# add shadow effect # explosion effect pie chart is separated from from matplotlib import pyplot as plt explo= [0.3 seaborn' 0] # Control explosion effect plt.style.use ('seaborn') plt.figure (figsize= (15Power9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.pie (cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True) plt.show ()
Add a percentage to the pie chart
# add shadow effect # explosion effect pie chart is separated from from matplotlib import pyplot as plt explo= [0.3 seaborn' 0] # Control explosion effect plt.style.use ('seaborn') plt.figure (figsize= (15Power9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.pie (cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=0,autopct='%1.2f%%') plt.show ()
6. Rotate the pie chart at different angles
# the pie chart is rotated from matplotlib import pyplot as plt explo= [0.3 plt.figure 0] # Control explosion effect plt.style.use ('seaborn') plt.figure (figsize= (15Power9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.pie (cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%') plt.show ()
Add edge lines to the pie chart
# add an edge line to the pie chart from matplotlib import pyplot as plt explo= [0.3 edgecolor] # Control the explosion effect plt.style.use ('seaborn') plt.figure (figsize= (15Ling 9)) plt.rcParams.update ({' font.family': "Microsoft YaHei"}) plt.title ("China Box Office 2021TOP9") plt.pie (cnbodfgbsort.BO,labels=cnbodfgbsort.index,explode=explo,shadow=True,startangle=45,autopct='%1.2f%%',wedgeprops= {"edgecolor": "black"}) plt.show ()
But I don't feel very obvious myself.
Grouping pie chart data
# categorize the data according to box office labels= ['> 20000,000,000,000,000,000,000,000,000,000,000 respectively.