熊猫如何与条形图一起显示百分比

问题描述 投票:0回答:1

我已经为下面的数据绘制了条形图

                Total Monthly Actual Hours  Total Monthly Work Hours
Activity Month
Apr-19          35381.25                    42592
May-19          31722.50                    44528
Jun-19          27708.50                    38720
Jul-19          34283.50                    44528
Aug-19          21359.90                    42592

到目前为止,我的代码

display(dfWorkActual)

dfWorkActual.plot(kind='bar')
plt.ylabel('Work Hours')
plt.xlabel('Month')
plt.title("Total Monthly Work Hours & Total Actual Work Hours vs Month")

Chart

现在我想将实际总小时数占每月总小时数的百分比相加。

例如:

enter image description hereenter image description hereenter image description here

请告知

pandas matplotlib percentage
1个回答
0
投票

您可以做的是用这种方式在绘图上注释一些文本

for x,y,tex in zip(x_axis, abs_value, perc_value):
        t = ax.text(x, 
                    y, 
                    f"{tex:.2f} %", 
                    horizontalalignment='center',
                    verticalalignment='center',
                    size = 11.5, 
                    bbox = dict(boxstyle="round", 
                                fc="w", 
                                ec='#414141', 
                                linewidth=1.4))

其中x_axis是带有列所在位置的列表。 abs_value是带有列高的列表,perc_value是带有百分比的列表。我在文本中添加了其他元素,例如bbox将创建一个带有百分比的圆形白框。试着使用参数以获得最佳效果。如您所见,我将文本f"{tex:.2f} %"放置在坐标(x, y)上。希望对您有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.