0

I am new to the "visualization in Pandas" world and I am trying to visualize a set of data as a scattered chart.

Initially my dataframe looks like this:

country_code  'ABW'  'AFG'  'AGO' 'AIA'  'ALB'
0               0      0      0    43     106 
1               10     10     100  50     100

I transposed it and I have the following dataframe df_transposed:

                 kc0  kc1
 country_code
  'ABW'           0   10
  'AFG'           0   10
  'AGO'           0   100
  'AIA'          43   50
  'ALB'          106  100

I am now plotting this transposed dataframe into a beautiful chart that works fine, but I would like to have the country_code as a label on each point.

This is what I currently get by using:

df_transposed.plot.scatter(x='kc0', y='kc1')

enter image description here

Is it possible to add the country code as a label to all the dots in this chart?

Rigerta
  • 3,959
  • 15
  • 26

1 Answers1

1

You might try this

data_df = pd.read_csv('data.csv')
nd = data_df.shape[0]
with plt.style.context('seaborn'):      # 'fivethirtyeight'      
    fig = plt.figure(figsize=(20,8)) ;   
    for i in range(nd):
        x = data_df.loc[i,data_df.columns[1]]
        y = data_df.loc[i,data_df.columns[2]]
        tx= data_df.loc[i,data_df.columns[0]]
        plt.plot(x, y, marker='o',ms=29, c='orange',alpha = 0.6)
        plt.text(x,y, tx, fontsize=18)
    plt.title('Nobel Laureates vs Chocolate Consumption', fontweight='bold',fontsize=35)
    plt.margins(0.1)
    plt.show()

gives this: enter image description here

Full example here

pyano
  • 1,885
  • 10
  • 28