2017-04-24 96 views
0

我在这段代码中有一个错误,但似乎无法找到它。第一列没有显示百分比。应该是一个快速修复!第一列不会显示百分比

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

而且,如果你还可以在“你的”计划和基准列之间添加一行柱分离,这将是伟大的!非常感谢家伙!

回答

0

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

for p in ax.patches: 

并且它将显示0th值的%标签。

要添加一条垂直线0th和酒吧之间1st你可以在x=0.5

line = plt.axvline(x=0.5) 

对于线请参考性质的docs进一步的造型绘制axvline,即

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

太棒了!谢谢。任何机会我们可以将线条变成黑色,褪色(部分透明)并以虚线形式显示? –