python xytext_更改xytext以防止批注重叠

所以我有一些代码可以生成一个甜甜圈图,但问题是有些情况下注释会因值而重叠。下面是代码和问题。在import pandas as pd

import numpy as np

import matplotlib.pyplot as plt

def donut_chart(val):

df_vals = pd.DataFrame.from_dict(val, orient='index')

labels = df_vals.index.tolist()

fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(aspect="equal"))

color = ['grey']*20

color[0] = 'red'

wedges, texts, junk = ax.pie(df_vals[0:4], counterclock = True,

wedgeprops=dict(width=0.6, linewidth = 2, edgecolor = 'w'),

startangle=90, colors=color,

autopct='%1.0f%%',

pctdistance=0.75,

textprops={'fontsize': 14})

bbox_props = dict(boxstyle="square,pad=0.3", fc="w", ec="w", lw=0.72)

kw = dict(xycoords='data', textcoords='data', arrowprops=dict(arrowstyle="-"),

bbox=bbox_props, zorder=0, va="center")

for i, p in enumerate(wedges):

ang = (p.theta2 - p.theta1)/2. + p.theta1

y = np.sin(np.deg2rad(ang))

x = np.cos(np.deg2rad(ang))

horizontalalignment = {-1: "right", 1: "left"}[int(np.sign(x))]

connectionstyle = "angle,angleA=0,angleB={}".format(int(ang))

kw["arrowprops"].update({"connectionstyle": connectionstyle})

ax.annotate(labels[i], xy=(x, y), xytext=(1.2*np.sign(x), 1.2*y),

horizontalalignment=horizontalalignment, **kw, size=14)

#centre_circle = plt.Circle((0,0),0.5, fc='white',linewidth=1.25)

#fig.gca().add_artist(centre_circle)

plt.axis('equal')

plt.show()

plt.close()

val = {'Label A':50, 'Label B':2, 'Label C':1, 'Label D':0.5}

donut_chart(val)

问题:

a4b0c3c98d2381dc6f32c0f11055ae66.png

我想做的是创造这样的东西:

48676d8d8171fbe259531f7fc4fe731d.png

关键点似乎是改变xytext中的y值,这样标签就不会重叠,但我一直在想这是如何实现的,甚至是它是否可行。在

有什么想法吗?在

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值