最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python使用Matplotlib模塊的pie()函數(shù)實(shí)現(xiàn)繪制餅形圖

 更新時(shí)間:2023年11月16日 10:34:46   作者:蟲無涯  
這篇文章主要為大家詳細(xì)介紹了 Python 如何使用 Matplotlib 模塊的pie()函數(shù)繪制餅形圖,文中有詳細(xì)的代碼講解,對(duì)大家學(xué)習(xí)或工作有一定的幫助,感興趣的可以了解一下

1 模塊安裝

  • 先安裝matplotlib
pip install matplotlib

  • 安裝numpy模塊,安裝matplotlib時(shí)候就已經(jīng)安裝這個(gè)依賴了,所以不用裝了,當(dāng)然也可以獨(dú)立安裝:

  • 安裝pandas
pip install numpy

2 實(shí)現(xiàn)思路

  • 數(shù)據(jù)存放在excel中,對(duì)指定數(shù)據(jù)進(jìn)行分析,所以需要用到pandas;
  • 對(duì)指定數(shù)據(jù)分析后繪制餅形圖,需要用到Matplotlib模塊的pie()函數(shù);
  • 對(duì)以下指定excel內(nèi)容進(jìn)行分析;
  • 分析用戶購(gòu)買區(qū)域情況占比,以下數(shù)據(jù)僅為參考,無真實(shí)意義,把以下內(nèi)容保存為data.xlsx:
用戶	    金額	    地址
user001	130.22	重慶
user002	55.64	江蘇省
user003	33	    江蘇省
user004	158.23	重慶
user005	124.56	安徽省
user006	33.26	山東省
user007	59.9	吉林省
user008	34.9	福建省
user009	45.36	山西省 
user010	35.23	河南省
user011	123.25	廣東省
user012	44.25	河北省
user013	58.26	廣東省
user014	83.79	貴州省
user015	59.99	廣東省
user016	63.12	福建省
user017	110.78	湖北省
user018	120.21	上海
user019	42.59	山東省
user020	78.99	山西省
user021	1150	浙江省
user022	66	    廣東省
user023	1256	安徽省
user024	36.3	廣東省
user025	54.89	廣東省
user026	164.89	廣東省
user027	45.78	廣東省
user028	126.45	廣東省
user029	47.35	河南省
user030	135.79	廣東省
user031	159.23	廣東省
user032	61.45	廣東省
user033	110.41	廣東省
user034	298.12	江蘇省
user035	102.23	云南省
user036	70.59	上海
user037	159.87	廣東省
user038	143.21	浙江省
user039	89.9	廣東省
user040	49.9	浙江省
user041	52.3	山東省
user042	89.4	江西省
user043	59.21	北京
user044	37.77	廣東省
user045	33.29	廣東省
user046	36.19	貴州省
user047	159.9	福建省
user048	49.9	四川省
user049	45.6	廣東省
user050	149.8	廣東省

3 pie()函數(shù)說明

  • 實(shí)現(xiàn)這個(gè)功能,主要使用了matplotlib 中 pyplot里的pie()函數(shù);
  • pie()函數(shù)部分源碼:
# Autogenerated by boilerplate.py.  Do not edit as changes will be lost.
@_copy_docstring_and_deprecators(Axes.pie)
def pie(
        x, explode=None, labels=None, colors=None, autopct=None,
        pctdistance=0.6, shadow=False, labeldistance=1.1,
        startangle=0, radius=1, counterclock=True, wedgeprops=None,
        textprops=None, center=(0, 0), frame=False,
        rotatelabels=False, *, normalize=None, data=None):
    return gca().pie(
        x, explode=explode, labels=labels, colors=colors,
        autopct=autopct, pctdistance=pctdistance, shadow=shadow,
        labeldistance=labeldistance, startangle=startangle,
        radius=radius, counterclock=counterclock,
        wedgeprops=wedgeprops, textprops=textprops, center=center,
        frame=frame, rotatelabels=rotatelabels, normalize=normalize,
        **({"data": data} if data is not None else {}))
  • 參數(shù)說明:
參數(shù)說明
x繪圖數(shù)據(jù)
explode指定餅形圖突出顯示的部分
labels餅形圖標(biāo)簽說明
colors餅形圖的填充色
autopct自動(dòng)添加百分比顯示
pctdistance設(shè)置百分比標(biāo)簽與圓心的距離
shadow是否添加餅形圖的陰影效果
labeldistance設(shè)置各扇形標(biāo)簽與圓心的距離
startangle設(shè)置餅形圖的初始擺放角度
radius設(shè)置餅圖的半徑
counterclock是否讓餅圖逆時(shí)針顯示
wedgeprops設(shè)置餅圖內(nèi)外邊界的屬性,如邊界線粗細(xì)和顏色
textprops設(shè)置餅圖文本屬性,如字體大小和顏色
center餅圖的中心點(diǎn)位置,默認(rèn)原點(diǎn)
frame是否顯示餅形圖后的圖框

4 實(shí)現(xiàn)過程

4.1 導(dǎo)入包

import pandas as pd
from matplotlib import pyplot as plt

4.2 定義一個(gè)類

  • 為了代碼整潔和可讀性,我們定義過一個(gè)類TestPie():
  • 類初始化:
class TestPie():
    def __init__(self):
        super(TestPie, self).__init__()

4.3 讀取數(shù)據(jù)并處理

 # 讀取數(shù)據(jù)
self.data_path = './data.xlsx'
self.data_content = pd.DataFrame(pd.read_excel(self.data_path))

# 獲取地址信息
self.address = self.data_content['地址']
self.data_content['省'] = self.address

# 獲取序號(hào)/地址/金額信息
self.content = self.data_content.groupby(['省'], as_index=False)["金額"].sum().reset_index()
# print(self.content)

# 降序排序
self.content01 = self.content.sort_values(['金額'], ascending=False)
self.content02 = self.content01.head(5)  # 讀取前5行

4.4 定義餅圖繪制方法

  • 定義方法:
    def test_pic(self):
        """餅形圖"""
  • 解決中文亂碼問題:
# 解決中文亂碼
plt.rcParams['font.sans-serif'] = ['SimHei']
  • 設(shè)置餅圖大?。?/li>
# 調(diào)節(jié)圖形大小
plt.figure(figsize=(3, 6))
  • 定義標(biāo)簽:
labels = self.content02['省'].values.tolist()
  • 設(shè)置餅形圖每塊的值:
sizes = self.content02['金額'].values.tolist()
  • 設(shè)置餅形圖每塊的顏色:
colors = ['cyan','darkorange','lawngreen','pink','gold']
  • 餅圖繪制:
patches, l_text, p_text = plt.pie(sizes,
                labels=labels,
                colors=colors,
                labeldistance=1, 
                autopct='%.1f%%', 
                startangle=90,
                radius=0.5,
                center=(0.3, 0.3),
                textprops={'fontsize': 8, 'color': 'k'},
                pctdistance=0.7)
  • 設(shè)置圖例,標(biāo)題等:
# 設(shè)置x,y軸刻度一致,這樣餅圖才能是圓的
plt.axis('equal')
plt.legend(loc='lower left', bbox_to_anchor=(-0.1, 0.8))
plt.title('購(gòu)買力分析')

5 完整源碼

# -*- coding:utf-8 -*-
# 作者:蟲無涯
# 日期:2023/11/15
# 文件名稱:test_pie.py
# 作用:Matplotlib模塊的pie()函數(shù)繪制餅形圖
# 聯(lián)系:VX(NoamaNelson)
# 博客:https://blog.csdn.net/NoamaNelson

import pandas as pd
from matplotlib import pyplot as plt


class TestPie():
    def __init__(self):
        super(TestPie, self).__init__()
        # 讀取數(shù)據(jù)
        self.data_path = './data.xlsx'
        self.data_content = pd.DataFrame(pd.read_excel(self.data_path))

        # 獲取地址信息
        self.address = self.data_content['地址']
        self.data_content['省'] = self.address

        # 獲取序號(hào)/地址/金額信息
        self.content = self.data_content.groupby(['省'], as_index=False)["金額"].sum().reset_index()
        # print(self.content)

        # 降序排序
        self.content01 = self.content.sort_values(['金額'], ascending=False)
        self.content02 = self.content01.head(5)  # 讀取前5行

    def test_pic(self):
        """餅形圖"""
        # 解決中文亂碼
        plt.rcParams['font.sans-serif'] = ['SimHei']

        # 調(diào)節(jié)圖形大小
        plt.figure(figsize=(3, 6))
        # 定義標(biāo)簽
        labels = self.content02['省'].values.tolist()
        # 設(shè)置餅形圖每塊的值
        sizes = self.content02['金額'].values.tolist()
        # 設(shè)置餅形圖每塊的顏色
        colors = ['cyan', 'darkorange', 'lawngreen', 'pink', 'gold']

        patches, l_text, p_text = plt.pie(sizes,
                labels=labels, 
                colors=colors, 
                labeldistance=1, 
                autopct='%.1f%%',
                startangle=90, 
                radius=0.5, 
                center=(0.3, 0.3),
                textprops={'fontsize': 8, 'color': 'k'},
                pctdistance=0.7) 
                
        plt.axis('equal')
        # 顯示圖例
        plt.legend(loc='lower left', bbox_to_anchor=(-0.1, 0.8)

        # 添加圖標(biāo)題
        plt.title('購(gòu)買力分析')
        plt.grid()
        plt.show()


if __name__ == "__main__":
    result = TestPie()
    result.test_pic()

效果顯示:

以上就是Python使用Matplotlib模塊的pie()函數(shù)實(shí)現(xiàn)繪制餅形圖的詳細(xì)內(nèi)容,更多關(guān)于Python Matplotlib餅形圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Python語言中的Selenium環(huán)境搭建

    Python語言中的Selenium環(huán)境搭建

    本文主要介紹了Python語言中的Selenium環(huán)境搭建,Python+Selenium這篇文章將不斷的持續(xù)更新和重構(gòu),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-08-08
  • python實(shí)現(xiàn)對(duì)列表中的元素進(jìn)行倒序打印

    python實(shí)現(xiàn)對(duì)列表中的元素進(jìn)行倒序打印

    今天小編就為大家分享一篇python實(shí)現(xiàn)對(duì)列表中的元素進(jìn)行倒序打印,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • anaconda如何創(chuàng)建和刪除環(huán)境

    anaconda如何創(chuàng)建和刪除環(huán)境

    這篇文章主要介紹了anaconda如何創(chuàng)建和刪除環(huán)境,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • 在python3.5中使用OpenCV的實(shí)例講解

    在python3.5中使用OpenCV的實(shí)例講解

    下面小編就為大家分享一篇在python3.5中使用OpenCV的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • 詳解在OpenCV中實(shí)現(xiàn)的圖像標(biāo)注技術(shù)

    詳解在OpenCV中實(shí)現(xiàn)的圖像標(biāo)注技術(shù)

    圖像標(biāo)注在計(jì)算機(jī)視覺中很重要,計(jì)算機(jī)視覺是一種技術(shù),它允許計(jì)算機(jī)從數(shù)字圖像或視頻中獲得高水平的理解力,并以人類的方式觀察和解釋視覺信息,本文將重點(diǎn)討論在OpenCV的幫助下創(chuàng)建這些注釋,感興趣的朋友一起看看吧
    2022-06-06
  • 淺談Django Admin的初步使用

    淺談Django Admin的初步使用

    本文主要介紹了淺談Django Admin的初步使用 ,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • 終端能到import模塊 解決jupyter notebook無法導(dǎo)入的問題

    終端能到import模塊 解決jupyter notebook無法導(dǎo)入的問題

    這篇文章主要介紹了在終端能到import模塊 而在jupyter notebook無法導(dǎo)入的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的實(shí)例代碼

    python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的實(shí)例代碼

    迭代器也是用來遍歷對(duì)象成員的,下面這篇文章主要給大家介紹了關(guān)于python遍歷迭代器自動(dòng)鏈?zhǔn)教幚頂?shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-01-01
  • 詳解Python Pyside6如何準(zhǔn)確嵌入可視化數(shù)據(jù)圖表

    詳解Python Pyside6如何準(zhǔn)確嵌入可視化數(shù)據(jù)圖表

    Pyside6是一款基于Qt框架的Python GUI開發(fā)庫(kù)。它提供了豐富的UI組件和功能,支持多種操作系統(tǒng)。本文主要介紹了Pyside6嵌入可視化數(shù)據(jù)圖表的方法,需要的可以參考一下
    2023-05-05
  • pygame實(shí)現(xiàn)彈球游戲流程

    pygame實(shí)現(xiàn)彈球游戲流程

    這篇文章主要介紹了pygame實(shí)現(xiàn)彈球游戲流程,彈球游戲(Pinball Pro)是安卓手機(jī)上排名第一的彈球游戲,真實(shí)再現(xiàn)了一直以來最經(jīng)典的彈珠臺(tái)游戲的玩法
    2023-01-01

最新評(píng)論

临泽县| 万州区| 博湖县| 华容县| 凉山| 仲巴县| 昌平区| 铜山县| 崇义县| 全南县| 留坝县| 汝城县| 霍城县| 通州市| 水城县| 遵义县| 旬邑县| 建平县| 株洲县| 都兰县| 丹东市| 随州市| 铁岭市| 卓资县| 彭泽县| 绩溪县| 大兴区| 洪洞县| 马关县| 昌黎县| 达拉特旗| 九江县| 育儿| 南川市| 池州市| 民权县| 云南省| 湟源县| 金沙县| 吴忠市| 崇明县|