在sns relplot的facetgrid中注释文本 [英] Annotate text in facetgrid of sns relplot

查看:48
本文介绍了在sns relplot的facetgrid中注释文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下数据帧(mx):

 代码增长价值风险Mcap0 APOLLOHOSP 8 6高小1 ANUP 8 7高小2 SIS 4 6高中3 HAWKINCOOK 5 2中低4 NEULANDLAB 6 4低大5 ORIENTELEC 7 9中低6 AXISBANK 2 3中中7 DMART 4 1中大型8 ARVIND 2 10中小9 TCI 1 7高中10 MIDHANI 5 5低大11分6 4中12 COROMANDEL 9 9高小13 SBIN 10 3中号 

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.

解决方案

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()

这篇关于在sns relplot的facetgrid中注释文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆