gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:25:04 25 4
gpt4 key购买 nike

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

(该函数采用一个字符串,即 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com