Не удалось добавить метки в диаграмму matplotlib из фрейма данных - PullRequest
0 голосов
/ 15 февраля 2020

Я создал диаграмму в matplotlib, которая является подзаговором, и она дает цвета для каждой точки на основе другого столбца: enter image description here

Я хочу добавить метку к каждой точке из другого столбца - называется city, поэтому рядом с каждой точкой будет указываться, из какого города поступают данные.

enter image description here

Я нашел использование функции, которую я нашел здесь :

#PLOT 
fig = plt.figure(figsize = (12,8))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('population', fontsize = 15)
ax.set_ylabel('PM 2.5', fontsize = 15)
ax.set_title('Population vs population', fontsize = 20)
ax.annotation

##color the points according to the cluster analysis result
targets = df.pop_ndvi_pm_km6.unique()
colors = ['darkgreen', 'b', 'orange','purple','r','y']
for target, color in zip(targets,colors):
    #goes to line. and check of 40,70 and 100 are true or false, 3 times total, and if true gives the color.
    indicesToKeep = df['pop_ndvi_pm_km6'] == target
    ax.scatter(df.loc[indicesToKeep, 'population']
               , df.loc[indicesToKeep, 'pm 2.5']
               , c = color
               , s = 100)
ax.legend(targets)
ax.grid()

######The functuion I have tried to add the labels to the points########
def label_point(x, y, val, ax):
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
    for i, point in a.iterrows():
        ax.text(point['x'], point['y'], str(point['val']))

label_point(df['population'], df['PM 2.5'], df['City'], ax)

draw()

Но я получил ошибку

AttributeError: У объекта 'AxesSubplot' нет атрибута 'annotation'

My конечная цель: добавить метку к каждой точке на основе названия города, из которого поступают данные.

...