条形图Matplotlib中并排显示的绝对值和百分比值 [英] Absolute Values and Percentage Values Side by Side in Bar Chart Matplotlib
问题描述
对于您遇到的以下问题,我想依靠您的帮助.我一直在尝试将绝对值和百分比值并排放置,但没有成功.其中 53
和 47
是百分比值,将在括号之外,而 17
和 15
是绝对值,并且在括号内.绝对值和百分比值都是已知的,因此,无需进行任何计算即可获得它们.我将代码留在这里供您查看,我可以走多远.这是错误:
TypeError:__init __()获得了参数'xytext'的多个值
x_axis = ["MJO处于活动状态","MJO处于非活动状态"]y_axis = [53,47]无花果,ax = plt.subplots(figsize =(10,7))调色板= sns.color_palette([#55a868"])rects = sns.barplot(x_axis,y_axis,线宽= 0,颜色='#55a868')ax.tick_params(axis ='both',which ='major',pad = 1)plt.xlabel('MJO活动',fontsize = 24)plt.xticks(旋转=水平",大小= 20)ax.locator_params(axis ='y',integer = True)plt.ylabel('事件百分比(%)',fontsize = 24)plt.yticks(大小= 18)plt.ylim(0,60)plt.title('ONDJFMA-1996/2014',fontsize = 24)ax.get_xaxis().set_label_coords(0.5,-0.10)ax.get_yaxis().set_label_coords(-0.06,0.5)值= [17、15]v = 0对于rects.patches中的rect:rects.annotate(format(rect.get_height(),'.0f'),(rect.get_x()+ rect.get_width()/2.,1.0 * rect.get_height()),'%d(%d)'%(int(rect.get_height()),values [v]),ha ='center',va ='bottom',fontsize = 20,xytext =(0,0),textcoords ='offset points')v = v + 1plt.show()
您快到了.我们用 zip
链接矩形,百分比值和绝对值,然后使用
I would like to count on your help for the following problem that I have been facing. I've been trying, but without success, to put absolute values and percentage values side by side. Where 53
and 47
are percentage values and would be outside the parentheses and 17
and 15
are absolute values and would be inside the parentheses. Both absolute values and percentage values are already known, therefore, there is no need for any calculation to obtain them. I leave my code here for you to see how far I managed to go. This is the error:
TypeError: __init__() got multiple values for argument 'xytext'
x_axis = ["MJO Active","MJO Inactive"]
y_axis = [53,47]
fig, ax = plt.subplots(figsize=(10,7))
palette = sns.color_palette(["#55a868"])
rects = sns.barplot(x_axis,y_axis, linewidth = 0, color='#55a868')
ax.tick_params(axis='both', which='major', pad=1)
plt.xlabel('MJO Activity', fontsize=24)
plt.xticks(rotation="horizontal", size = 20)
ax.locator_params(axis='y', integer=True)
plt.ylabel('Percentage of Events (%)', fontsize=24)
plt.yticks(size = 18)
plt.ylim(0,60)
plt.title('ONDJFMA - 1996/2014',fontsize=24)
ax.get_xaxis().set_label_coords(0.5,-0.10)
ax.get_yaxis().set_label_coords(-0.06,0.5)
values = [17, 15]
v = 0
for rect in rects.patches:
rects.annotate(format(rect.get_height(), '.0f'),
(rect.get_x() + rect.get_width() / 2., 1.0*rect.get_height()),
'%d(%d)' % (int(rect.get_height()), values[v]), ha = 'center', va='bottom', fontsize = 20,
xytext = (0, 0),
textcoords = 'offset points')
v = v + 1
plt.show()
You were nearly there. We link bar rectangles, percentage values, and absolute values with zip
, then use f-string formatting for the display:
...
values = [17, 15]
for rect, perc, vals in zip(rects.patches, y_axis, values):
rects.annotate(f"{perc}% ({vals})", (rect.get_x() + rect.get_width() / 2., rect.get_height()),
ha='center', va='center', fontsize=20, color='black', xytext=(0, 10),
textcoords='offset points')
plt.tight_layout()
plt.show()
Sample output:
这篇关于条形图Matplotlib中并排显示的绝对值和百分比值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!