我正在尝试调整一个图表的图例 . 两组的值表示为条形图,而两组的平均值由水平线表示 . 我有调整水平线的句柄长度的问题 - handlelength参数似乎在这里不起作用 .

如下图所示,句柄对于“人口”来说太长了 . 生成图表的代码如下 . 有人能帮助解决这个问题吗?

enter image description here

n_groups = 5

    fig, ax = plt.subplots(figsize=(4,4))

    index = np.arange(n_groups)
    bar_width = 0.35
    opacity = 0.4

    means_g1 = np.array([ 7.76349192,  8.1247542 ,  7.49507752,  7.27215192,  5.66707868])
    means_g2 = np.array([ 0.64601404,  0.43165984,  0.87665884,  1.12260548,  1.93469648])


    ax.bar(index, means_g1, bar_width,
                     alpha=opacity,
                     color='b',
                     error_kw=error_config,
                     label='Group 1')

    ax.bar(index + bar_width, means_g2, bar_width,
                     alpha=opacity,
                     color='r',
                     error_kw=error_config,
                     label='Group 2')

    ax.plot(index+bar_width,(means_g1+means_g2)/2,'_',ms=25,mew=1.5,label='Population')

    ax.legend(loc = 0, fancybox = True, framealpha = 0.2, prop={'size':10},
             #handlelength=3
             )

    ax.set_xlabel('Number of states')
    ax.set_xticks(index + bar_width)
    ax.set_xticklabels(('2', '4', '6', '8', '10'))
    ax.set_xlim(-bar_width,5)
    ax.set_ylim([0,10])

    plt.tight_layout()
    plt.show()