Python繪制餅圖、圓環(huán)圖的實(shí)例
Python繪制餅圖、圓環(huán)圖
下面是我們作圖需要使用到的數(shù)據(jù)(數(shù)據(jù)是個(gè)人虛構(gòu)的、不代表各品牌真實(shí)銷售數(shù)據(jù))
| 品牌 | 子品牌 | 銷量 | 總銷量/臺 |
| 比亞迪 | 唐 | 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.其它類中展開
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構(gòu)建一個(gè)簡單的批處理GUI工具
在許多工作流程中,我們都會(huì)遇到需要重復(fù)執(zhí)行的一系列任務(wù),手動(dòng)執(zhí)行這些步驟可能既繁瑣又容易出錯(cuò),自動(dòng)化此類序列可以顯著提高生產(chǎn)力,所以本文小編給大家分享了如何使用Python構(gòu)建一個(gè)簡單的批處理GUI工具,需要的朋友可以參考下2025-04-04
pymysql 插入數(shù)據(jù) 轉(zhuǎn)義處理方式
今天小編就為大家分享一篇pymysql 插入數(shù)據(jù) 轉(zhuǎn)義處理方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python使用PyAutoGUI實(shí)現(xiàn)桌面自動(dòng)化功能
PyAutoGUI 是一個(gè)跨平臺的桌面自動(dòng)化工具,能夠模擬鼠標(biāo)點(diǎn)擊、鍵盤輸入、屏幕截圖與圖像識別,適用于重復(fù)性桌面任務(wù),本集通過代碼+截圖+輸出日志的實(shí)戰(zhàn)形式,帶你掌握從基礎(chǔ)操作到復(fù)雜任務(wù)的全流程自動(dòng)化,需要的朋友可以參考下2025-07-07
Python編程中的文件讀寫及相關(guān)的文件對象方法講解
這篇文章主要介紹了Python編程中的文件讀寫及相關(guān)的文件對象方法講解,其中文件對象方法部分講到了對文件內(nèi)容的輸入輸出操作,需要的朋友可以參考下2016-01-01
詳解如何使用Pandas創(chuàng)建有效且可復(fù)制的代碼
Pandas作為一種多功能和強(qiáng)大的工具而屹立不倒,其直觀的數(shù)據(jù)結(jié)構(gòu)和廣泛的功能使其成為無數(shù)數(shù)據(jù)專業(yè)人士和愛好者的首選,本文將使用Pandas創(chuàng)建有效且可復(fù)制的代碼,感興趣的可以了解下2024-11-11
pandas數(shù)據(jù)篩選和csv操作的實(shí)現(xiàn)方法
這篇文章主要介紹了pandas數(shù)據(jù)篩選和csv操作的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例
這篇文章主要介紹了python實(shí)現(xiàn)sm2和sm4國密(國家商用密碼)算法的示例,幫助大家使用python加密文件,感興趣的朋友可以了解下2020-09-09
使用Python向DataFrame中指定位置添加一列或多列的方法
今天小編就為大家分享一篇使用Python向DataFrame中指定位置添加一列或多列的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
Python桌面應(yīng)用開發(fā)實(shí)戰(zhàn)之PyQt的安裝使用
這篇文章主要給大家介紹了關(guān)于Python桌面應(yīng)用開發(fā)實(shí)戰(zhàn)之PyQt的安裝使用,PyQt是一個(gè)功能強(qiáng)大的Python庫,用于創(chuàng)建圖形用戶界面(GUI)應(yīng)用程序,需要的朋友可以參考下2023-08-08

