在饼图上方绘制线

时间:2018-11-10 07:53:27

标签: python matplotlib line

我试图在以下饼图中绘制属于三个内部层的数据之间的线:

enter image description here

图中的黑线显示了其中一条线应该在的位置。

但是,我不知道如何设置正确的长度。实际上,使用plt.plot([1-0.3, 1+0.4+0.5], [0, 0])似乎对起点(x[0])的变化很敏感,但对终点(x[1])的变化不敏感:它总是在同一点结束与我设置为x[1]的值无关。有人知道如何正确绘制这条线吗?

这是我的代码:

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',]


pie_properties = ax.pie(group_vals, radius=1, colors=group_colors, 
       wedgeprops=dict(width=0.3, edgecolor='k'))

pie_properties = ax.pie(info_vals.flatten(), radius=1+0.4, colors=info_colors,
       wedgeprops=dict(width=0.4, edgecolor='w'))


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

#line plotting
plt.plot([1-0.3, 1+0.4+0.5], [0, 0], linewidth=2, color='k')



ax.set(aspect="equal")
plt.show()

1 个答案:

答案 0 :(得分:1)

似乎该行被剪切了。

可以向

matplotlib.axes.Axes.plot传递配置裁剪的选项。

ax.plot([1.4, 0], [0, 0], linewidth=2, color='k', clip_on=False)

相关问题