第一列不显示百分比

First Column won't Show Percentage

我在这段代码的某个地方有一个错误,但似乎找不到它。第一列未显示百分比。应该是一个快速修复!

import pandas as pd
import matplotlib.pyplot as plt

s = pd.Series(
    [8, 24, 21, 23, 24],
    index = ["Your Plan", "Benchmark", "Region", "Industry", "Size"]
)

#Set descriptions:
plt.title("INSERT TITLE HERE, PLEASE")
plt.ylabel('Percentage of EEs Waiving')

#Set tick colors:
ax = plt.gca()
ax.tick_params(axis='x', colors='black')
ax.tick_params(axis='y', colors='black')

#Plot the data:
my_colors = ['#ffc000','#305496', '#8ea9db', '#b4c6e7', '#D9E1F2']  #red, green, blue, black, etc.

s.plot(
    kind="bar",
    color=my_colors,
)

# Rotate the desired axis (or undo the rotation)
ax.set_xticklabels(ax.xaxis.get_majorticklabels(), rotation=0)

# Set the Y Axis Limits
ax.set_ylim([0,100])

#Adds data labels to top of bars
for p in ax.patches[1:]:
    h = p.get_height()
    x = p.get_x()+p.get_width()/2.
    if h != 0:
        ax.annotate("%g" % p.get_height()+'%', xy=(x,h), xytext=(0,8), rotation=0,
                    textcoords="offset points", ha="center", va="bottom")

plt.show()
plt.savefig("PlanOffered.png")

而且,如果您还可以在 "Your Plan" 和基准列之间添加一行来分隔列,那就太好了!非常感谢你们!

Adds data labels to top of bars1st 元素而不是 0th 元素开始的循环中。将其更改为

for p in ax.patches:

它会显示 0th 值的 % 标签。

要在 0th1st 柱之间添加一条垂直线,您可以在 x=0.5

处绘制 axvline
line = plt.axvline(x=0.5)

有关线条的进一步样式,请参阅 docs 中的属性,即

  • line.linestyle='dashed'
  • line.color = 'black'