Python繪制餅圖、圓環(huán)圖的實(shí)例
Python繪制餅圖、圓環(huán)圖
下面是我們作圖需要使用到的數(shù)據(jù)(數(shù)據(jù)是個(gè)人虛構(gòu)的、不代表各品牌真實(shí)銷售數(shù)據(jù))
品牌 | 子品牌 | 銷量 | 總銷量/臺(tái) |
比亞迪 | 唐 | 10000 | 29000 |
比亞迪 | 宋 | 9000 | 29000 |
比亞迪 | 元 | 6000 | 29000 |
比亞迪 | 海豚 | 4000 | 29000 |
特斯拉 | Model3 | 8000 | 17500 |
特斯拉 | ModelS | 6000 | 17500 |
特斯拉 | ModelY | 3500 | 17500 |
大眾 | 朗逸 | 3000 | 12000 |
大眾 | 速騰 | 3000 | 12000 |
大眾 | 高爾夫 | 6000 | 12000 |
豐田 | 卡羅拉 | 6000 | 12000 |
豐田 | 雷凌 | 4000 | 12000 |
豐田 | 凱美瑞 | 2000 | 12000 |
奇瑞 | 艾瑞澤5 | 1000 | 2000 |
奇瑞 | 艾瑞澤8 | 1000 | 2000 |
領(lǐng)克 | 領(lǐng)克01 | 1000 | 1000 |
1.各品牌的銷售數(shù)量餅圖
import pandas as pd from matplotlib import pyplot as plt #解決中文亂碼 plt.rcParams['font.sans-serif'] = ['SimHei'] data=pd.read_excel(r'汽車銷量數(shù)據(jù)數(shù)據(jù).xlsx',sheet_name='Sheet1') #根據(jù)各品牌去重 data_total_sale=data.loc[:,["品牌","總銷量"]].drop_duplicates() print(data_total_sale) out: 品牌 總銷量 0 比亞迪 29000 4 特斯拉 17500 7 大眾 12000 10 豐田 12000 13 奇瑞 2000 15 領(lǐng)克 1000 fig, ax = plt.subplots(figsize=(10, 7)) ax.pie(data_total_sale['總銷量'],labels=data_total_sale['品牌'],autopct='%1.1f%%') plt.show()
2.各品牌銷售數(shù)據(jù)圓環(huán)圖
import pandas as pd from matplotlib import pyplot as plt #解決中文亂碼 plt.rcParams['font.sans-serif'] = ['SimHei'] data=pd.read_excel(r'汽車銷量數(shù)據(jù)數(shù)據(jù).xlsx',sheet_name='Sheet1') #根據(jù)各品牌去重 data_total_sale=data.loc[:,["品牌","總銷量"]].drop_duplicates() total_sale=data_total_sale['總銷量'].sum() fig, ax = plt.subplots(figsize=(10, 7)) ax.pie(data_total_sale['總銷量'], radius=1.5, wedgeprops={'width': 0.7}, labels = data_total_sale['品牌'], autopct='%3.2f%%', pctdistance=0.75) #保留2位小數(shù) plt.text(0, 0, total_sale, ha='center', va='center', fontsize=28) plt.show()
3.將數(shù)據(jù)少的合并為其它
import pandas as pd from matplotlib import pyplot as plt #解決中文亂碼 plt.rcParams['font.sans-serif'] = ['SimHei'] data=pd.read_excel(r'汽車銷量數(shù)據(jù)數(shù)據(jù).xlsx',sheet_name='Sheet1') #根據(jù)各品牌去重 data_total_sale=data.loc[:,["品牌","總銷量"]].drop_duplicates() others=["奇瑞","領(lǐng)克"] data_new=data_total_sale.loc[~data['品牌'].isin(others)] other_sum=data_total_sale['總銷量'].loc[data['品牌'].isin(others)].sum() data_new=data_new.append({"品牌":'其它',"總銷量":other_sum},ignore_index=True) fig, ax = plt.subplots(figsize=(10, 7)) ax.pie(data_new['總銷量'],labels=data_new['品牌'],autopct='%1.1f%%') plt.show()
4.其它類中展開(kāi)
import pandas as pd from matplotlib import pyplot as plt from matplotlib.patches import ConnectionPatch from matplotlib import cm #解決中文亂碼 plt.rcParams['font.sans-serif'] = ['SimHei'] data=pd.read_excel(r'汽車銷量數(shù)據(jù)數(shù)據(jù).xlsx',sheet_name='Sheet1') #根據(jù)各品牌去重 data_total_sale=data.loc[:,["品牌","總銷量"]].drop_duplicates() others=["奇瑞","領(lǐng)克"] data_new=data_total_sale.loc[~data['品牌'].isin(others)] other_sum=data_total_sale['總銷量'].loc[data['品牌'].isin(others)].sum() data_new=data_new.append({"品牌":'其它',"總銷量":other_sum},ignore_index=True) data_other=data_total_sale.loc[data['品牌'].isin(others)] fig = plt.figure(figsize=(10,4)) ax1 = fig.add_subplot(1,2,1) ax1.pie(data_new['總銷量'],labels=data_new['品牌'],autopct='%1.1f%%') ax2 = fig.add_subplot(1,2,2) ax2.pie(data_other['總銷量'],labels=data_other['品牌'],autopct='%1.1f%%',radius=0.5,wedgeprops=dict(width=0.3, edgecolor='w')) theta1, theta2 = ax1.patches[-1].theta1+15, ax1.patches[-1].theta2-15 center, r = ax1.patches[-1].center,ax1.patches[-1].r x = r*np.cos(np.pi/180*theta1)+center[0] y = np.sin(np.pi/180*theta1)+center[1] con1 = ConnectionPatch(xyA=(0, 0.5),xyB=(x,y), coordsA=ax2.transData, coordsB=ax1.transData,axesA=ax2,axesB=ax1) x = r * np.cos(np.pi / 180 * theta2) + center[0] y = np.sin(np.pi / 180 * theta2) + center[1] con2 = ConnectionPatch(xyA=(-0.1, -0.49), xyB=(x, y), coordsA=ax2.transData, coordsB=ax1.transData, axesA=ax2, axesB=ax1) for con in [con1, con2]: con.set_color('gray') ax2.add_artist(con) con.set_linewidth(1) fig.subplots_adjust(wspace=0) plt.show()
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程批量實(shí)現(xiàn)md5加密pdf文件
這篇文章主要介紹了Python編程批量實(shí)現(xiàn)md5加密pdf文件,文章基于python的相關(guān)資料展開(kāi)詳細(xì)內(nèi)容介紹,具有一定的參考價(jià)值需要的小伙伴可以參考一下2022-04-04pandas使用get_dummies進(jìn)行one-hot編碼的方法
今天小編就為大家分享一篇pandas使用get_dummies進(jìn)行one-hot編碼的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Flask入門教程實(shí)例:搭建一個(gè)靜態(tài)博客
這篇文章主要介紹了Flask入門教程實(shí)例:搭建一個(gè)靜態(tài)博客,本文主要介紹flask框架的環(huán)境配置以及一個(gè)靜態(tài)博客胡搭建實(shí)例,需要的朋友可以參考下2015-03-03Pycharm學(xué)習(xí)教程(6) Pycharm作為Vim編輯器使用
這篇文章主要為大家詳細(xì)介紹了最全的Pycharm學(xué)習(xí)教程第六篇,Pycharm作為Vim編輯器使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05Python import用法以及與from...import的區(qū)別
這篇文章主要介紹了Python import用法以及與from...import的區(qū)別,本文簡(jiǎn)潔明了,很容易看懂,需要的朋友可以參考下2015-05-05使用Python中PDB模塊中的命令來(lái)調(diào)試Python代碼的教程
這篇文章主要介紹了使用Python中PDB模塊中的命令來(lái)調(diào)試Python代碼的教程,包括設(shè)置斷點(diǎn)來(lái)修改代碼等、對(duì)于Python團(tuán)隊(duì)項(xiàng)目工作有一定幫助,需要的朋友可以參考下2015-03-03Python將GIF動(dòng)圖轉(zhuǎn)換為Base64編碼字符串的步驟詳解
在Web開(kāi)發(fā)中,有時(shí)需要將圖像文件(如GIF動(dòng)圖)轉(zhuǎn)換為Base64編碼的字符串,以便在HTML或CSS中直接嵌入圖像數(shù)據(jù),本文給大家就介紹了一個(gè)簡(jiǎn)單的教程,教你如何使用Python將GIF動(dòng)圖轉(zhuǎn)換為Base64編碼的字符串,需要的朋友可以參考下2025-02-02