【问题标题】:How to display percentage along with bar chart如何与条形图一起显示百分比
【发布时间】:2020-02-29 13:00:15
【问题描述】:

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

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

现在我想添加每月总小时数中总实际小时数的百分比。

例如:

请指教

【问题讨论】:

  • 能把你做的代码加进去吗?
  • 当然。我会编辑帖子

标签: python pandas matplotlib percentage


【解决方案1】:

对于条形图的注释,您可以参考此处的 matplotlib 文档中的示例。

https://matplotlib.org/3.1.1/gallery/lines_bars_and_markers/barchart.html#sphx-glr-gallery-lines-bars-and-markers-barchart-py

fig = plt.figure(figsize=(15,10))
ax = plt.gca()
width = 0.35
rects1 = ax.bar(df.index-width/2, df.A, width)
rects2 = ax.bar(df.index+width/2, df.B, width)
for r1, r2 in zip(rects1, rects2):
    h1 = r1.get_height()
    h2 = r2.get_height()
    percent = int(h1 * 100 / h2)
    ax.annotate('{}%'.format(percent),
                    xy=(r1.get_x() + r1.get_width() / 2, h1),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')
    ax.annotate('100%',
                    xy=(r2.get_x() + r2.get_width() / 2, h2),
                    xytext=(0, 3),  # 3 points vertical offset
                    textcoords="offset points",
                    ha='center', va='bottom')

plt.show()

fig = plt.figure(figsize=(15,10))
ax = plt.gca()
width = 0.35
rects1 = ax.bar(df.index, df.A, width)
rects2 = ax.bar(df.index, df.B, width, bottom=df.A)
for r1, r2 in zip(rects1, rects2):
    h1 = r1.get_height()
    h2 = r2.get_height()
    percent = int(h1 * 100 / h2)
    ax.annotate('{}%'.format(percent),
                    xy=(r1.get_x() + r1.get_width() / 2, h1/2),
                    xytext=(0, 0),
                    textcoords="offset points",
                    ha='center', va='bottom')
    ax.annotate('100%',
                    xy=(r2.get_x() + r2.get_width() / 2, h1+h2/2),
                    xytext=(0, 0), 
                    textcoords="offset points",
                    ha='center', va='bottom')

plt.show()

【讨论】:

    【解决方案2】:

    你可以用这种方式在图上注释一些文本

    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) 处。 希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2019-02-04
      • 1970-01-01
      • 2022-12-01
      • 2020-12-04
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      相关资源
      最近更新 更多