饼状图
- 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()