简体   繁体   English

怎样才能在一个条plot的条内写文本?

[英]How can write text inside bars of a bar plot?

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下面的代码用X=BoroughY=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: 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:对于酒吧 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')

在此处输入图像描述

You can use plt.annotate to display "Complaint Type".您可以使用plt.annotate显示“投诉类型”。

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 在 matplotlib 条形图上添加值标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM