2017-04-24 3 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

0th 대신 1st 요소로 시작하는 루프에서 Adds data labels to top of bars.

for p in ax.patches: 

으로 변경하면 0th 값의 % 라벨이 표시됩니다. 당신의 라인 docs에 속성을 참조하시기 바랍니다 더 스타일에 대한 x=0.5

line = plt.axvline(x=0.5) 

에서 axvline을 플롯 할 수 있습니다 바 0th 사이 1st

수직 라인을 추가하려면 즉

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

굉장! 고맙습니다. 우리가 선을 검은 색, 퇴색 (부분적으로 투명 함) 및 대시 선 형식으로 만들 수있는 기회가 있습니까? –