1

I am trying to label several points in my scatter plot in python using matplotlib.

My x and y are primary and secondary columns. I want to be able to annotate and increase the size of the points by the country name column (example label China and USA).

My table is called df6 and it contains several columns. I want to label 5 points from the country name column.

enter image description here

x = df6.primary
y = df6.secondary
z = df6.Country Name
plt.title('Primary & Secondary')
plt.xlabel('Primary')
plt.ylabel('Secondary')
plt.show()

enter image description here

jon
  • 11
  • 4

1 Answers1

0

I made up a simple DataFrame to show how to do this; the main "trick" is to create a mask with df['Country Name'].isin( … ) and the getting the (x,y,label) values where the mask is True:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

data={
    'x':np.arange(0,7,1),
    'y':np.arange(0,7,1),
    'Country Name':['Aruba','Germany','Austria','China','USA','England','Italy']
}

df=pd.DataFrame(data)

plt.scatter(df['x'],df['y'],c="b",s=3**2,zorder=2)

selectedCountries=['Germany','USA', 'China']
mask=(df['Country Name'].isin( selectedCountries ))
plt.scatter(df['x'][mask],df['y'][mask],c="r",s=5**2,zorder=3)

for (x,y,label) in df[['x','y','Country Name']][mask].values:
    plt.text(x,y,label,zorder=4)

plt.show()
Asmus
  • 5,117
  • 1
  • 16
  • 21