【问题标题】:Organizing text on pie charts at matplotlib在 matplotlib 上组织饼图上的文本
【发布时间】:2014-02-03 15:49:30
【问题描述】:

我已经学习了绘制饼图的基础知识(通过此处的教程和示例),但我无法将字幕放在图表上方(我可能需要减小饼图的大小,但我该怎么做做吗?)。我还想将我添加的额外文本框放在饼图的右下角或左下角。如果有人能给出提示,那就太好了!

(该函数接受一个字符串,它是通道的名称,然后是一个包含 4 个百分比的列表,一个表示质量的 int 和一个标志 save_figures,如果我想保存图形)

def plot_channel(channel,percentages, mass, save_figures):
    # build a rectangle in axes coords
    left, width = .25, .5
    bottom, height = .25, .5
    right = left + width
    top = bottom + height

    channel = ''.join(i for i in channel if i in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')



    nu_energy , d_plus_p_energy, e_energy, gamma_energy  = percentages  

    # The slices will be ordered and plotted counter-clockwise.
    labels = [r'$E_{\nu} / E_{tot}$ = %.3f'%nu_energy, 
              r'$E_{d+p} / E_{tot}$ = %.3f'%d_plus_p_energy,
              r'$E_{e} / E_{tot}$ = %.3f'%e_energy,
              r'$E_{\gamma} / E_{tot}$ = %.3f'%gamma_energy]
    sizes = [nu_energy , d_plus_p_energy, e_energy, gamma_energy]
    colors = ['gold','red','green', 'lightskyblue']
    explode = (0.1, 0,0,0)
    patches, texts = plt.pie(sizes, colors=colors)#, startangle=90) ** not working for some reason
    plt.legend(patches, labels, loc = "best")
    E_gamma_e = e_energy + gamma_energy
    plt.text(right, bottom, 
             r'$E_{\gamma + e} / E_{tot}$ = %.3f'%E_gamma_e,
             horizontalalignment='left',
             verticalalignment='bottom',
             bbox=dict(facecolor='white', alpha=0.5), fontsize=30)  
    #plt.pie(sizes, explode=explode, labels=labels, colors=colors,
            #autopct='%1.1f%%', shadow=True)
    # Set aspect ratio to be equal so that pie is drawn as a circle.
    plt.axis('equal')
    plt.suptitle(r'DM DM $\rightarrow$ $%s$ + $%s$'%(channel,channel),position=(left,top),
                 bbox=dict(facecolor='0.8',), fontsize=30)
    plt.tight_layout()
    if save_figures:
        plt.savefig("./figures/energy_distribution_for_channel_{}.png".format(channel))
    else:
        plt.show()

    plt.close()

【问题讨论】:

    标签: python matplotlib pie-chart


    【解决方案1】:

    试试这个:

    import matplotlib.pyplot as plt
    
    channel,percentages, mass = "ab",[0.2,0.2,0.1,0.5], 10
    
    # build a rectangle in axes coords
    left, width = .25, .5
    bottom, height = .25, .5
    right = left + width
    top = bottom + height
    
    channel = ''.join(i for i in channel if i in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
    nu_energy , d_plus_p_energy, e_energy, gamma_energy  = percentages  
    
    # The slices will be ordered and plotted counter-clockwise.
    labels = [r'$E_{\nu} / E_{tot}$ = %.3f' % nu_energy, 
              r'$E_{d+p} / E_{tot}$ = %.3f' % d_plus_p_energy,
              r'$E_{e} / E_{tot}$ = %.3f' % e_energy,
              r'$E_{\gamma} / E_{tot}$ = %.3f' %gamma_energy]
    sizes = [nu_energy , d_plus_p_energy, e_energy, gamma_energy]
    colors = ['gold','red','green', 'lightskyblue']
    explode = (0.1, 0,0,0)
    patches, texts = plt.pie(sizes, colors=colors)#, startangle=90) ** not working for some reason
    plt.legend(patches, labels, loc = "best")
    E_gamma_e = e_energy + gamma_energy
    #plt.pie(sizes, explode=explode, labels=labels, colors=colors,
            #autopct='%1.1f%%', shadow=True)
    # Set aspect ratio to be equal so that pie is drawn as a circle.
    plt.axis('equal')
    plt.title(r'DM DM $\rightarrow$ $%s$ + $%s$'%(channel,channel),position=(0.5,1),bbox=dict(facecolor='0.8',), fontsize=30)
    plt.text(-1,-0.98, r'$E_{\gamma + e} / E_{tot}$ = %.3f'%E_gamma_e, bbox=dict(facecolor='white', alpha=0.5), fontsize=14)  
    plt.tight_layout()
    
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多