向图中添加数字

时间:2019-03-07 01:33:43

标签: python pandas graph

我创建了一个漂亮的棒棒糖图,在y轴上有国家,在x上有值。

Data = {'Country': ['UK','Ireland', 'South Africa','Botswana','Italy','Greece'], 
        'Assessment': [170,170, 170, 65,64,53]}

df= pd.DataFrame(data = data)

ordered_fa = df.sort_values(by='Assessment')

my_range=range(1,len(df.index)+1)

a4_dims = (10, 12)
f, ax = plt.subplots(figsize=a4_dims)
sns.despine(offset=50)

plt.hlines(y=my_range, xmin=0, xmax=ordered_fa['Assessment'], color='skyblue')
plt.plot(ordered_fa['Assessment'], my_range, "o")

# Add titles and axis names
plt.yticks(my_range, ordered_fa['Country'])
plt.title("Assessments per month (2018)", loc='left')

我想在每个报告数字的“ o”的末尾添加一个数字标签。

该怎么做?

1 个答案:

答案 0 :(得分:0)

请使用ax.text(x, y, 'str')

import matplotlib.pyplot as plt
import pandas as pd


Data = {'Country': ['UK','Ireland', 'South Africa','Botswana','Italy','Greece'], 
        'Assessment': [170,170, 170, 65,64,53]}

df= pd.DataFrame(data = Data)

ordered_fa = df.sort_values(by='Assessment')

my_range=range(1,len(df.index)+1)

a4_dims = (10, 12)
f, ax = plt.subplots(figsize=a4_dims)
# sns.despine(offset=50)

plt.hlines(y=my_range, xmin=0, xmax=ordered_fa['Assessment'], color='skyblue')
plt.plot(ordered_fa['Assessment'], my_range, "o")

# Add titles and axis names
plt.yticks(my_range, ordered_fa['Country'])
plt.title("Assessments per month (2018)", loc='left')

for i in my_range:
        x = 7 - i
        y = Data['Assessment'][i-1]
        ax.text(y + 2,x,str(y))



plt.show()