【matplotlib】3.餅狀圖

餅狀圖

  • plt.pie()
matplotlib.pyplot.pie(x, explode=None, labels=None, colors=None, autopct=None, pctdistance=0.6, shadow=False, labeldistance=1.1, startangle=None, radius=None, counterclock=True, wedgeprops=None, textprops=None, center=(0, 0), frame=False, rotatelabels=False, *, data=None)

1.參數

參數 含義 類型
x 要顯示的數據 array-like
labels 扇形對應的標籤 list, optional, default: None
explode 如果不是None,則是len(x)數組,該數組指定每個楔形突出多少。 array-like, optional, default: None
autopct 在圖上顯示數據 None (default), string, or function, optional
shadow 陰影 bool, optional, default: False

2.例子

2.1 例1 普通的餅狀圖

import matplotlib.pyplot as plt

def test1():
    labels = ['a', 'b', 'c', 'd', 'e']
    x = [20,30,40,50,60]

    plt.pie(x, labels=labels)
    plt.show()

if __name__ == '__main__':
    test1()

在這裏插入圖片描述

2.2 顯示每一個扇形的數據

import matplotlib.pyplot as plt

def test1():
    labels = ['a', 'b', 'c', 'd', 'e']
    x = [20,30,40,50,60]

    plt.pie(x, autopct="%0.2f%%", labels=labels)
    plt.show()

if __name__ == '__main__':
    test1()

在這裏插入圖片描述

2.3 突出一部分扇形

import matplotlib.pyplot as plt

def test1():
    labels = ['a', 'b', 'c', 'd', 'e']
    x = [20,30,40,50,60]
    explode = (0.5,0,0,0,0)

    plt.pie(x, autopct="%0.2f%%", labels=labels, explode=explode)
    plt.show()


if __name__ == '__main__':
    test1()

在這裏插入圖片描述
explode對應的數越大,代表突出的那一部分離中心越遠。下圖是0.5時的情況:
在這裏插入圖片描述

2.4 添加陰影

import matplotlib.pyplot as plt

def test1():
    labels = ['a', 'b', 'c', 'd', 'e']
    x = [20,30,40,50,60]
    explode = (0.5,0,0,0,0)

    plt.pie(x, autopct="%0.2f%%", labels=labels, explode=explode, shadow=True)
    plt.show()

if __name__ == '__main__':
    test1()

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章