餅狀圖
- 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()