Home > Net >  I am unable to label the data points on the graph using matplotlib
I am unable to label the data points on the graph using matplotlib

Time:07-05

This is the code that I have been writing, but unable to add labels to the data points. Have tried multiple ways but getting error one after the other!! The data set in 9th line: 'country' is to be used as labelling. I want to label the 1st and last data point. Please Help!

```python
import pandas as pd
import numpy as np
from sklearn.cluster import KMeans
import matplotlib.pyplot as plt

data = pd.read_csv('happy_income1.csv')
happy = data['happyScore']
satis = data['avg_satisfaction']
country = data['country']

# Zapping 2 arrays together
satis_happy = np.column_stack((satis,happy))

# Sorting
data.sort_values('avg_satisfaction', inplace=True) #Sorting Data Column

# Filtering
satisfied = data[data['avg_satisfaction']>4] #Making Section as per requirement
print(satisfied)

# Making clusters as required
k_res = KMeans(n_clusters=3).fit(satis_happy)
cluster = k_res.cluster_centers_
print(cluster)

# Plotting
fig, week4 = plt.subplots()
week4.scatter(x=happy, y=satis)
week4.scatter(x=cluster[:,0], y=cluster[:,1], s=9999, alpha=0.25)
week4.set_xlabel('Happiness')
week4.set_ylabel('Satisfaction')
week4.set_title('Happiness versus Satisfaction')

# Labelling
# ----------------------------------------------

plt.show()
```

CSV File Link: enter image description here

  • Related