如何使用Matplotlib在条形图上显示值

时间:2019-01-28 23:19:47

标签: python matplotlib visualization

import pandas as pd
import numpy as np

df_survey=pd.read_csv('Topic_Survey_Assignment(1).csv',index_col=0)
df_survey=df_survey.sort_values('Very interested',ascending=False)
df_survey.columns = list(map(str, df_survey.columns))

df_survey1=df_survey.div(2233).round(2)
df_survey1

import matplotlib as mpl
import matplotlib.pyplot as plt 
import numpy as np

df_survey1.plot(kind='bar',figsize=(20,8),width=0.8,color=['#5cb85c','#5bc0de','#d9534f'],edgecolor='none')  
plt.xlabel('Data Science Areas') 
plt.ylabel('Percentage of Respondents\'interest') 
plt.title('Percentage of Respondents\' interest in Data Science Areas', size=16)
plt.legend(fontsize=14)

我明白了

enter image description here

我想在每个条上方显示百分比值。救命!我是Python的新手,所以不知道。

1 个答案:

答案 0 :(得分:0)

您可以在https://matplotlib.org/api/text_api.html?highlight=text#module-matplotlib.text上使用matplotlib的文本功能将文本放置在每个条的底部,而仅使用一些'{}'.format(percentage)从代码中动态更新百分比吗?不过,您将必须使用此方法显式定义每个文本的x和y位置,或开发诸如x = i/number_of_bars之类的公式。