python 饼图笔记 两个饼图 双饼图 复合饼图 两个饼图 环形图

复合饼图
fig = plt.figure(figsize=(10,4))
ax1 = fig.add_subplot(121)
a = kcf.groupby('一级分类').总金额.sum().sort_values(ascending=False)
ax1.pie(a, labels=a.index, autopct='%.2f%%',startangle=-110, explode=[0,0.1],
        colors=['#508cb4','#d7e4ef'])
plt.title('一级分类销售额分类汇总')
ax2 = fig.add_subplot(122)
a = kcf[kcf.一级分类=='饮料'].groupby('二级分类').总金额.sum().sort_values(ascending=False)
ax2.pie(a, radius=0.9, labels=a.index, autopct='%.2f%%', colors=['#265f95', '#508cb4', '#89b5ce', '#c5d8e7', '#e7eff7'])
plt.title('饮料品类下的销售额分类汇总')

#使用ConnectionPatch画出两个饼图的间连线
#先得到饼图边缘的数据
theta1, theta2 = ax1.patches[0].theta1+20, ax1.patches[0].theta2-20
center, r = ax1.patches[0].center,ax1.patches[0].r
x = r*np.cos(np.pi/180*theta2)+center[0]
y = np.sin(np.pi/180*theta2)+center[1]
con = ConnectionPatch(xyA=(0, 0.9),xyB=(x,y),
                     coordsA='data', coordsB='data',axesA=ax2,axesB=ax1)
con.set_linewidth(2)
con.set_color=([0,0,0])
ax2.add_artist(con)

#画出下边缘的连线
x = r*np.cos(np.pi/180*theta1)+center[0]
y = np.sin(np.pi/180*theta1)+center[1]
con = ConnectionPatch(xyA=(0,-0.9),xyB=(x,y),
                     coordsA='data', coordsB='data',axesA=ax2,axesB=ax1)
con.set_linewidth(2)
con.set_color=([0,0,0])
ax2.add_artist(con)

在这里插入图片描述参考:python生成复合饼图

环形图

加入参数wedgeprops=dict(width=0.3, edgecolor='w')

a = pe[pe.PID.isin(ksf.PID.drop_duplicates())].性别.value_counts()
plt.pie(a, labels=a.index, autopct='%.1f%%', colors=['#8ebad9','#f1a4bd'],
       wedgeprops=dict(width=0.3, edgecolor='w'))

在这里插入图片描述

标签重叠、显示部分标签

在这里插入图片描述

方法一:
  1. 参数 explode 部分环块分开
  2. 参数 autopct 只显示百分比大于1%的数据标签
def my_autopct(pct):
    return ('%.1f%%' % pct) if pct > 1 else ''
plt.pie(a, labels=a.index, explode=[0,0,0.1,0.2,0,0,0,0], colors=colorSeries[a.index], autopct=my_autopct, wedgeprops=dict(width=0.3, edgecolor='w'),pctdistance=0.58)
plt.title('统一', fontsize=15)

在这里插入图片描述

方法二:
调整饼图图表位置

参数 pctdistance 是数据标签离圆心的距离

ax2.pie(a, labels=a.index, autopct='%.1f%%',textprops=dict(fontsize=11),
       wedgeprops=dict(width=0.3, edgecolor='w'),pctdistance=0.55)

在这里插入图片描述#### 饼图图标重叠怎么办
更改位置
更改前:
在这里插入图片描述

更改后:

在这里插入图片描述

  • 1
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值