matplotlib饼状图案例

说明

实现下图效果的饼状图:

代码

import matplotlib.pyplot as plt

plt.rcParams['font.family'] = ['MicroSoft YaHei']

# 设置画布背景色
plt.rcParams['axes.facecolor'] = '#1e1c54'

f, ax = plt.subplots()
# 设置窗口颜色
f.patch.set_facecolor('#1e1c54')

# 数据
datas = [7800, 35900, 46800, 25631, 17583, 1563, 1059]
# 标签
labels = ['1万以下', '1万-2万', '2万-3万', '3万-4万', '4万-5万', '5万-6万', '7万-15万']
# 设置颜色
colors = ['#5470c6', '#9fe080', '#ffdc60', '#ff7070', '#79cbeb', '#73c0de', '#ff915a']
# 显示百分比
autopct = '%d%%'
# autopct = '%0.2f%%'
# 绘制饼状图,x轴是小时,y轴是温度
ax.pie(datas, colors=colors)

leg = plt.legend(loc='upper left',labels = labels, bbox_to_anchor=(-0.4, 0, 0.5, 1))
for text in leg.get_texts(): # 获取图例中的文本对象
    text.set_color('w') # 设置图例文本的颜色

plt.title("访问网站来源分析",color='w',loc="left")
# 显示图
plt.show()

原文出处:/show_1GW2TpZ7JWM.html