Question

I've learned the basics of plotting pie charts (through the tutorial and examples here), but I don't manage to put the suptitle above the chart (I need maybe to reduce the pie chart size, but how do I do it?). I also want to place the extra text box that I added in the bottom right or left side of the pie chart. If someone can give a hint it would be great!

(The function takes a string which is the name of the channel, then a list with 4 percentages, an int for the mass and a flag save_figures if I want to save the figure)

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

Was it helpful?

Solution

Try this:

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top