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?

0

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.