python - 改变 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)
问题:
我想做的是创建这样的东西:
关键似乎是改变 xytext 中的 y 值,因此标签不会重叠,但我不知道这可能如何实现,甚至是否可能。
有任何想法吗?
解决方案
更新代码
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 = ax.pie(df_vals[0:5], counterclock = True,
wedgeprops=dict(width=0.6, linewidth = 1, edgecolor = 'w'),
startangle=90, colors=color,
textprops={'fontsize': 14})
bbox_props = dict(boxstyle="square,pad=0", 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=((i/10) + 1.1*np.sign(x), (i/10) + 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':0.2, 'Label D':0.2,'Label E':0.2}
donut_chart(val)
区别
ax.annotate(labels[i], xy=(x, y), xytext=((i/10) + 1.1*np.sign(x), (i/10) + y),
horizontalalignment=horizontalalignment, **kw, size=14)
推荐阅读
- android - 如何以 xamarin 形式绘制刮刮卡?
- angular - Angular 5 Material Side Menu 在 iOS 中不起作用
- swift - Swift 4 + Alamofire:解析自定义结构数组
- oledb - excel工作簿的OleDb方案未在架构中显示行
- angular - 基于 MAC ID 的登录
- npm - 如何安装 react-admin 的 `next` 分支?
- office365 - Officejs 插件撰写模式属性更改
- python - 具有两个变量的 Python 逆指数函数
- php - 一个 PDF 文件中的多个图像
- python - 不同的自相关归一化值与 statsmodels