Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 标签';放置在饼图中_Python_Matplotlib_Label - Fatal编程技术网

Python 标签';放置在饼图中

Python 标签';放置在饼图中,python,matplotlib,label,Python,Matplotlib,Label,我正在努力使下面的饼图更漂亮: 特别是,我对标签有一个明显的问题。我想旋转它们并将它们居中放置在相应的饼图切片上方,但我只能旋转它们。此外,旋转需要通过设置进行大量调整 pie\u属性[1][0]。为每个标签设置不同度数值的旋转(30) 有没有一种方法可以自动实现,或者至少以一种更简单的方式实现 这是我的密码: import matplotlib.pyplot as plt import numpy as np fig, ax = plt.subplots() ### Data stru

我正在努力使下面的饼图更漂亮:

特别是,我对标签有一个明显的问题。我想旋转它们并将它们居中放置在相应的饼图切片上方,但我只能旋转它们。此外,旋转需要通过设置进行大量调整
pie\u属性[1][0]。为每个标签设置不同度数值的旋转(30)

有没有一种方法可以自动实现,或者至少以一种更简单的方式实现

这是我的密码:

import matplotlib.pyplot as plt
import numpy as np


fig, ax = plt.subplots()

### Data structure: 
# Network type:
group_names = ['Group 1', 'Group 2', 'Group 3']
group_vals = [43,49,38]
group_colors = ['w']*3

#Information level:
info_names = ['Subgroup 1', 'Subgroup 2', \
              'Subgroup 1', 'Subgroup 2',\
              'SSubgroup 1', 'Subgroup 2']
info_vals = np.array([[27,16],[26,23],[14,24]])
my_blue = [x/255 for x in [30,144,255]]
info_colors = [my_blue,'gray',\
               my_blue,'gray',\
               my_blue,'gray']

corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],\
                     [10,5,5,3,3], [10,5,5,1,2],\
                     [4,4,2,2,2], [12,2,2,1,7]])
pale_green = [x/255 for x in [152,251,152]]
pale_red = [x/255 for x in [240,128,128]]
pale_gray = [x/255 for x in [169,169,169]]
corr_colors = ['green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',\
               'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',\
               'green',pale_green,pale_gray,pale_red,'red', 'green',pale_green,pale_gray,pale_red,'red',]

#inner layer
pie_properties = ax.pie(group_vals, radius=1, colors=group_colors, 
       labels=group_names, labeldistance=0.7,
       wedgeprops=dict(width=0.3, edgecolor='k'))
pie_properties[1][0].set_rotation(-45) #<===rotation
pie_properties[1][1].set_rotation(90)
pie_properties[1][2].set_rotation(-135)

#middle layer
pie_properties = ax.pie(info_vals.flatten(), radius=1+0.4, colors=info_colors,
       labels=info_names, labeldistance=0.7,
       wedgeprops=dict(width=0.4, edgecolor='w'))
pie_properties[1][0].set_rotation(-45)
pie_properties[1][1].set_rotation(15)
pie_properties[1][2].set_rotation(65)
pie_properties[1][3].set_rotation(125)
pie_properties[1][4].set_rotation(-160)
pie_properties[1][5].set_rotation(-125)

#outer layer
ax.pie(corr_vals.flatten(), radius=1+0.4+0.5, colors=corr_colors,
       wedgeprops=dict(width=0.5, edgecolor='w'))


ax.set(aspect="equal")
plt.show()
导入matplotlib.pyplot作为plt
将numpy作为np导入
图,ax=plt.子批次()
###数据结构:
#网络类型:
组名称=['group 1'、'group 2'、'group 3']
组值=[43,49,38]
组_颜色=['w']*3
#信息水平:
info_name=['子组1','子组2'\
‘第1分组’、‘第2分组’\
'SSubgroup 1','子组2']
info_vals=np.array([[27,16],[26,23],[14,24])
my_blue=[x/255代表[30144255]]
信息颜色=[我的蓝色,'灰色'\
我的天蓝色,灰色\
我的_蓝,'灰']
corr_vals=np.数组([10,3,7,3,4],[4,4,2,2]\
[10,5,5,3,3], [10,5,5,1,2],\
[4,4,2,2,2], [12,2,2,1,7]])
淡绿色=[x/255表示[152251152]中的x]
淡红色=[x/255表示[240128128]中的x]
浅灰色=[x/255表示[169]中的x]
颜色=['绿色'、淡绿色、淡灰色、淡红色、'红色'、'绿色'、淡绿色、淡灰色、淡红色、'红色'\
“绿色”、“淡绿色”、“淡灰色”、“淡红色”、“红色”、“绿色”、“淡绿色”、“淡灰色”、“淡红色”、“红色”\
‘绿色’、淡绿色、淡灰色、淡红色、‘红色’、‘绿色’、淡绿色、淡灰色、淡红色、‘红色’,]
#内层
饼图属性=ax.pie(组值,半径=1,颜色=组颜色,
标签=组名称,标签距离=0.7,
wedgeprops=dict(宽度=0.3,边色=k'))

pie_属性[1][0]。设置_旋转(-45)#我想你可以用一点三角测量来计算所需的角度。由于标签放置在饼图楔块的平均角度范围内,因此计算角度很容易。根据文本是在下半部分还是上半部分,可以加或减90度

import matplotlib.pyplot as plt
import numpy as np

fig, ax = plt.subplots()

group_names = ['Group 1', 'Group 2', 'Group 3']
group_vals = [43,49,38]
group_colors = ['w']*3

info_names = ['Subgroup 1', 'Subgroup 2', \
              'Subgroup 1', 'Subgroup 2',\
              'Subgroup 1', 'Subgroup 2']
info_vals = np.array([[27,16],[26,23],[14,24]])
my_blue = np.array([30,144,255])/255
info_colors = [my_blue,'gray']*3

corr_vals = np.array([[10,3,7,3,4], [4,4,4,2,2],\
                     [10,5,5,3,3], [10,5,5,1,2],\
                     [4,4,2,2,2], [12,2,2,1,7]])
pale_green = np.array([152,251,152])/255
pale_red   = np.array([240,128,128])/255
pale_gray  = np.array([169,169,169])/255
corr_colors = ['green',pale_green,pale_gray,pale_red,'red']*6

def rotatetext(text):
    angle = np.rad2deg(np.arctan2(*text.get_position()[::-1]))
    text.set(rotation = angle - 90*np.sign(angle), 
             rotation_mode="anchor", ha="center", va="center")

#inner layer
pie_properties = ax.pie(group_vals, radius=1/1.9, colors=group_colors, 
       labels=group_names, labeldistance=.8,
       wedgeprops=dict(width=0.3/1.9, edgecolor='k'))

for text in pie_properties[1]:
    rotatetext(text)

#middle layer
pie_properties = ax.pie(info_vals.flatten(), radius=(1+0.4)/1.9, colors=info_colors,
       labels=info_names, labeldistance=.82,
       wedgeprops=dict(width=0.4/1.9, edgecolor='w'))

for text in pie_properties[1]:
    rotatetext(text)

#outer layer
ax.pie(corr_vals.flatten(), radius=(1+0.4+0.5)/1.9, colors=corr_colors,
       wedgeprops=dict(width=0.5/1.9, edgecolor='w'))

ax.set(xlim=(-1,1), ylim=(-1,1))
ax.set(aspect="equal")
plt.show()


但是,最佳径向位置需要手动调整。这里我分别选择了0.80和0.82,但这取决于图形和字体大小。

Hi,您知道如何为标签添加尾随空间吗?我在外层也添加了带有数字的标签。为了使它们更好地居中,我需要在其中一些上添加一些尾随空格。我尝试了各种解决方案:
'2\t'
'2'.ljust(2)
'+'2'
'$\$2
,但都不起作用。尾随空格总是被删除。这听起来好像与嵌套饼图无关。如果在本网站的其他地方找不到解决方案,你能用一个简单的例子来说明这个问题吗?