python - 在 matplotlib / pie 中对齐组标签
问题描述
是否可以在堆叠的甜甜圈图中对齐顶部组级别 - 以便标签位于饼图段内并遵循饼图的曲线 - 而不是水平的?
在我的例子中——右上角“这是一个长组标签”(是的,我拼错了)——这可以在外部段内并遵循段的曲线吗?
import numpy as py
import seaborn as sns
group_names=['This is a long group lable', 'groupB', 'groupC']
group_size=[1,1,1]
subgroup_names=['This is a long sub group label', 'Label 2', 'label 3', 'Label 4', 'Label 5','Label 6', 'Label 7', 'Label 8', 'Label 9']
subgroup_size=[1,1,1,1,1,1,1,1,1]
inner_name =["This is the inner label"]
inner_group=[1]
# Create colors
a, b, c, d = [plt.cm.Blues, plt.cm.Reds, plt.cm.Greens,plt.cm.Blues]
# Outer Ring (outside)
fig, ax = plt.subplots()
ax.axis('equal')
mypie, _ = ax.pie(group_size, radius=5, labels=group_names,labeldistance=0.95, colors=[a(0.6), b(0.6), c(0.6)] )
plt.setp( mypie, width=0.5, edgecolor='black')
# Label Ring (Inside)
mypie2, _ = ax.pie(subgroup_size, radius=4.5, labels=subgroup_names, labeldistance=0.9, colors=[a(0.5), a(0.5), a(0.5), b(0.5), b(0.5),b(0.5), c(0.5),c(0.5), c(0.5)])
plt.setp( mypie2, width=0.5, edgecolor='black')
plt.margins(0,0)
# 4th Ring (Inside)
mypie3, _ = ax.pie(subgroup_size, radius=4, colors=[a(0.1), a(0.1), a(0.1), b(0.1), b(0.9),b(0.9), c(0.1),c(0.1), c(0.1)])
plt.setp( mypie3, width=0.5, edgecolor='black')
plt.margins(0,0)
# 3rd Ring (Inside)
mypie4, _ = ax.pie(subgroup_size, radius=3.5, colors=[a(0.1), a(0.9), a(0.1), b(0.1), b(0.9),b(0.9), c(0.1),c(0.9), c(0.1)])
plt.setp( mypie4, width=0.5, edgecolor='black')
plt.margins(0,0)
# 2nd Ring (Inside)
mypie5, _ = ax.pie(subgroup_size, radius=3, colors=[a(0.9), a(0.9), a(0.1), b(0.9), b(0.9),b(0.9), c(0.9),c(0.9), c(0.1)])
plt.setp( mypie5, width=0.5, edgecolor='black')
plt.margins(0,0)
# 1st Ring (Inside)
mypie6, _ = ax.pie(subgroup_size, radius=2.5, colors=[a(0.9), a(0.9), a(0.9), b(0.9), b(0.9),b(0.9), c(0.9),c(0.9), c(0.1)])
plt.setp( mypie6, width=0.5, edgecolor='black')
# Inner rin Ring (Inside)
mypie7, _ = ax.pie(inner_group, radius=2,labels=inner_name, labeldistance=-0, colors=["black"])
plt.setp( mypie7, width=2, edgecolor='black')
plt.margins(0,0)
# show it
plt.show()
解决方案
从官方参考资料中,我尝试了很多东西。使用以下代码,您可以设置注释的文本位置和角度。我通过反复试验手动设置值。您可能能够以更编程的方式做出响应。
# Libraries
import matplotlib.pyplot as plt
# Make data: I have 3 groups and 7 subgroups
group_names = ['This is a long group lable', 'groupB', 'groupC']
group_size = [1,1,1]
group_angle = [-32, 90, 220]
subgroup_names=['This is a long sub group label', 'Label 2', 'label 3', 'Label 4', 'Label 5','Label 6', 'Label 7', 'Label 8', 'Label 9']
subgroup_size=[1,1,1,1,1,1,1,1,1]
subgroup_angle = [-70,0,0,0,0,0,0,0,0]
# Create colors
a, b, c = [plt.cm.Blues, plt.cm.Reds, plt.cm.Greens]
# First Ring (outside)
fig, ax = plt.subplots()
ax.axis('equal')
mypie, txts = ax.pie(group_size, radius=5, labels=group_names, startangle=0, labeldistance=0.97, colors=[a(0.6), b(0.6), c(0.6)] )
for txt,agl in zip(txts, group_angle):
txt.set_ha('center')
txt.set_va('center')
txt.set_rotation(agl)
plt.setp(mypie, width=0.5, edgecolor='black')
# Second Ring (Inside)
mypie2, txts2 = ax.pie(subgroup_size, radius=4.5, labels=subgroup_names, labeldistance=0.95, colors=[a(0.5), a(0.5), a(0.5), b(0.5), b(0.5), b(0.5), c(0.5), c(0.5), c(0.5), c(0.5)])
for txt,agl in zip(txts2, subgroup_angle):
txt.set_ha('center')
txt.set_va('center')
txt.set_rotation(agl)
plt.setp(mypie2, width=0.5, edgecolor='black')
plt.margins(0,0)
# show it
plt.show()
推荐阅读
- xaml - StringFormat 中的 Xamarin XAML 多语言
- c# - 通过 Ajax 请求超时 Umbraco Azure
- c - 异或运算的C程序,scanf输入的运算符
- meshlab - 向 Hausdorff 距离图添加图例
- javascript - 将 Array 转换为 String 再转换回 Array JavaScript 错误
- python - *(星号)可以作为文字字符传递给python吗
- mongodb - MongoTemplate聚合投影,映射数组属性时出现异常
- python - 即使通过虚拟环境安装了flask_wtf,我的程序也没有被执行
- html - 在 RStudio 中,knitr markdown 输出在哪里?
- python - Pyomo TypeError:不可散列的类型:'EqualityExpression'