【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()

在这里插入图片描述

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