python - 在 matplotlib 的饼图上组织文本

标签 python matplotlib pie-chart

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

(该函数采用一个字符串,即 channel 名称,然后是一个包含 4 个百分比的列表,一个表示质量的整数,如果我想保存数字,则需要一个标志 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()

Example of one of the pie charts created by this code

最佳答案

试试这个:

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()

关于python - 在 matplotlib 的饼图上组织文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21531683/

相关文章:

sorting - g.Raphael 饼图对图表数据进行排序,而不是对颜色、url 进行排序

objective-c - 饼图的核心图轴集不起作用

python - 可以集成 Google AppEngine 和 Google Code 以进行持续集成吗?

python - 为自定义数据类型实现 sum()

python - 绘制从一组到另一组的复杂函数

python - 如何在更改参数后自动更改颜色

android - MPAndroidChart 图例自定义

python - Apache Airflow 导入错误 : cannot import name '_psutil_linux'

python - pd.df 基于列表中的值的 bool 掩码

python - 如何在 python 中绘制映射