简体   繁体   English

如何在 python 的条形图上添加值

[英]How to add values on bar chart in python

I made a bar chart in Python but I cannot add the values on each of the bar.我在 Python 中制作了一个条形图,但我无法在每个条形上添加值。 Does anyone know how to do it?有谁知道该怎么做?

yelp_area=df.groupby('Area').Yelp_rating.mean()

yelp_area.plot(kind='bar', rot=0, alpha=0.8, figsize=(10,6), color='r')

plt.xticks(rotation='vertical')

plt.ylabel('Average Rating on a 1-5 scale')

I attached the picture of the bar chart.我附上了条形图的图片。

You can use addlabels in it:您可以在其中使用addlabels

# importing library
import matplotlib.pyplot as plt
  
# function to add value labels
def addlabels(x,y):
    for i in range(len(x)):
        plt.text(i, y[i]//2,y[i], ha = 'center',
                 Bbox = dict(facecolor = 'white', alpha = .5))
  
if __name__ == '__main__':
    
    # creating data on which bar chart will be plot
    x = ["Engineering", "Hotel Managment", "MBA",
         "Mass Communication", "BBA", "BSc", "MSc"]
    y = [9330, 4050, 3030, 5500,
         8040, 4560, 6650]
      
    # setting figure size by using figure() function 
    plt.figure(figsize = (10,5))
    
    # making the bar chart on the data with color red
    plt.bar(x, y, color = 'red')
      
    # calling the function to add value labels
    addlabels(x, y)
      
    # giving title to the plot
    plt.title("College Admission")
      
    # giving X and Y labels
    plt.xlabel("Courses")
    plt.ylabel("Number of Admissions")
      
    # visualizing the plot
    plt.show()

Which gives you this result.- Attached Image.这给了你这个结果。 -附加图像。

You can refer here for more detailed information.您可以参考此处了解更多详细信息。

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

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