0

I have created chart in matplotlib that is subplot and it gives colores to each point based on another column: enter image description here

I want to add label to each point from another column- called city, so beside each point it will tell which city the data comes from.

enter image description here

I have found to use function that I have found 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()

But I have gotten the error

AttributeError: 'AxesSubplot' object has no attribute 'annotation'

My end goal: to add label to each point based on the name of the city that the data comes from.

Reut
  • 1,555
  • 4
  • 23
  • 55
  • 1
    Does this answer your question? [Matplotlib scatter plot with different text at each data point](https://stackoverflow.com/questions/14432557/matplotlib-scatter-plot-with-different-text-at-each-data-point) – shimo Feb 15 '20 at 09:17
  • I have tried this but also didn't work :( – Reut Feb 15 '20 at 09:22
  • 1
    Just deleting `ax.annotation` and `draw()`, worked for me. – shimo Feb 15 '20 at 09:41

0 Answers0