[英]How to add values on bar chart in python
我在 Python 中制作了一個條形圖,但我無法在每個條形上添加值。 有誰知道該怎么做?
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')
您可以在其中使用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()
這給了你這個結果。 -附加圖像。
您可以參考此處了解更多詳細信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.