Search code examples
pythonpandasmatplotlibannotationsseaborn

Annotate text in facetgrid of sns relplot


Using the following data frame (mx):

    code    Growth  Value   Risk    Mcap
0   APOLLOHOSP  8   6   High    small
1   ANUP    8   7   High    small
2   SIS 4   6   High    mid
3   HAWKINCOOK  5   2   Low mid
4   NEULANDLAB  6   4   Low large
5   ORIENTELEC  7   9   Low mid
6   AXISBANK    2   3   Medium  mid
7   DMART   4   1   Medium  large
8   ARVIND  2   10  Medium  small
9   TCI 1   7   High    mid
10  MIDHANI 5   5   Low large
11  RITES   6   4   Medium  mid
12  COROMANDEL  9   9   High    small
13  SBIN    10  3   Medium  large

dataframe

I am trying to create a sns relplot which should annotate the scatter plot points in respective facetgrid. However the out put i get looks something like this:

relplot

Here all the annotations are seen in the first facet and points in other facets don't have any annotations.

I have tried the following code:

p1 = sns.relplot(x="Growth", y="Value", hue="Risk",col="Mcap",data=mx,s=200,palette = ['r','g','y'])
ax = p1.axes[0,0]
for idx,row in mx.iterrows():
    x = row[1]
    y = row[2]
    text = row[0]
    ax.text(x+0.5,y,text, horizontalalignment='left')

please advise the modifications. thanks in advance.


Solution

  • The main problem is that you set ax = p1.axes[0,0], while ax should be p1.axes[0,colum_number] depending on the column number of the subplot the text has to go.

    Further, addressing row[0], row[1] etc. can make the code less readable and less easy to adapt when something changes. So, it's better to assign the row directly to some variables, as in text, x, y, _risk, mcap = row. Even more maintainable would be itertuples() instead of iterrows(), as illustrated in the code below.

    In order to make space for the name, you could widen the x-limits a bit to the right.

    import pandas as pd
    import seaborn as sns
    from matplotlib import pyplot as plt
    
    data = [['APOLLOHOSP', 8, 6, 'High', 'small'],
            ['ANUP', 8, 7, 'High', 'small'],
            ['SIS', 4, 6, 'High', 'mid'],
            ['HAWKINCOOK', 5, 2, 'Low', 'mid'],
            ['NEULANDLAB', 6, 4, 'Low', 'large'],
            ['ORIENTELEC', 7, 9, 'Low', 'mid'],
            ['AXISBANK', 2, 3, 'Medium', 'mid'],
            ['DMART', 4, 1, 'Medium', 'large'],
            ['ARVIND', 2, 10, 'Medium', 'small'],
            ['TCI', 1, 7, 'High', 'mid'],
            ['MIDHANI', 5, 5, 'Low', 'large'],
            ['RITES', 6, 4, 'Medium', 'mid'],
            ['COROMANDEL', 9, 9, 'High', 'small'],
            ['SBIN', 10, 3, 'Medium', 'large']]
    mx = pd.DataFrame(data=data, columns=["code", "Growth", "Value", "Risk", "Mcap"])
    
    plotnum = {'small': 0, 'mid': 1, 'large': 2}
    p1 = sns.relplot(x="Growth", y="Value", hue="Risk", col="Mcap", data=mx, s=200, palette=['r', 'g', 'y'])
    
    for ax in p1.axes[0]:
        ax.set_xlim(0.0, max(mx["Growth"]) + 1.9)
    for row in mx.itertuples():
        ax = p1.axes[0, plotnum[row.Mcap]]
        ax.text(row.Growth + 0.5, row.Value, row.code, horizontalalignment='left')
    plt.show()
    

    resulting plot