0

I need help to print the names in column "Complaint Type" inside each of the bars.
The below code plots bar chart with X=Borough and Y=Count
Kindly help me as there are three columns involved.

df1 = dict({'Borough':['BROOKLYN','QUEENS','MANHATTAN','BRONX','STATEN ISLAND','Unspecified'],
           'Complaint Type':['Blocked Driveway','Blocked Driveway','Noise - Street/Sidewalk','Blocked 
            Driveway','Illegal Parking','Illegal Parking'], 
           'Count':[28148,31644,20550,12755,4886,1040]})
df2=pd.DataFrame(df1)
df2

Output:

    **Borough         Complaint Type              Count**
0   BROOKLYN          Blocked Driveway            28148
1   QUEENS            Blocked Driveway            31644
2   MANHATTAN         Noise - Street/Sidewalk     20550
3   BRONX             Blocked Driveway            12755
4   STATEN ISLAND     Illegal Parking             4886
5   Unspecified       Illegal Parking             1040

For Bar Plot:

df2.plot(x ="Borough",y="Count",kind='bar',alpha=0.6,color='red',figsize=(7,7),title='Most frequent complaint type in each Borough')
plt.xlabel('Borough')
plt.ylabel('Count')

enter image description here

gboffi
  • 22,939
  • 8
  • 54
  • 85

1 Answers1

1

You can use plt.annotate to display "Complaint Type".

df2.plot(x ="Borough",y="Count",kind='bar',alpha=0.6,color='red',figsize=(7,7),
         title='Most frequent complaint type in each Borough')
plt.xlabel('Borough')
plt.ylabel('Count')

for i in range(0, len(df2['Borough'])):
    plt.annotate(str(df2['Complaint Type'][i]), xy=(i, (df2['Count'][i] / 2)), 
    ha='center', va='center', weight='bold', size=10)


Look at this post, too.
Adding value labels on a matplotlib bar chart

elena.kim
  • 930
  • 4
  • 12
  • 22