Python matplotlib绘制饼图
Python 碎片
共 3090字,需浏览 7分钟
·
2020-12-29 13:21
一、matplotlib绘制饼图
import matplotlib.pyplot as plt
election_data = {'Biden': 290, 'Trump': 214, 'Others': 538-290-214}
candidate = [key for key in election_data]
votes = [value for value in election_data.values()]
plt.figure(figsize=(10, 10), dpi=100)
plt.pie(votes, labels=candidate, autopct="%1.2f%%", colors=['c', 'm', 'y'],
textprops={'fontsize': 24}, labeldistance=1.05)
plt.legend(fontsize=16)
plt.title("2020年A国大选票数占比", fontsize=24)
plt.show()
二、matplotlib旋转饼图和突出显示
import matplotlib.pyplot as plt
election_data = {'Biden': 290, 'Trump': 214, 'Others': 538-290-214}
candidate = [key for key in election_data]
votes = [value for value in election_data.values()]
plt.figure(figsize=(10, 10), dpi=100)
explode = (0.1, 0, 0)
plt.pie(votes, labels=candidate, autopct="%1.2f%%", colors=['c', 'm', 'y'], textprops={'fontsize': 24},
labeldistance=1.05, explode=explode, startangle=90, shadow=True)
plt.legend(loc='upper right', fontsize=16)
plt.title("2020年A国大选票数占比", fontsize=24)
plt.axis('equal')
plt.show()
三、matplotlib绘制环形饼图
import matplotlib.pyplot as plt
election_data = {'Biden': 290, 'Trump': 214, 'Others': 538-290-214}
candidate = [key for key in election_data]
votes = [value for value in election_data.values()]
plt.figure(figsize=(10, 10), dpi=100)
explode = (0, 0, 0)
plt.pie(votes, labels=candidate, explode=explode, autopct="%1.2f%%", colors=['c', 'm', 'y'],
textprops={'fontsize': 24}, labeldistance=1.05, pctdistance=0.85, startangle=90)
plt.pie([1], radius=0.7, colors='w')
plt.legend(loc='upper right', fontsize=16)
plt.title("2020年A国大选票数占比", fontsize=24)
plt.axis('equal')
plt.show()
评论