LoginSignup
3
1

More than 3 years have passed since last update.

seaborn散布図の各点にラベルを付ける

Last updated at Posted at 2020-04-25

初投稿の42歳おじさん。
seabornに乗り換えたて。

困ったこと

散布図を書いて、idと各点の対応がわからなくてイマイチだった。
調べてみると、各点へのラベル付けもやり方があるらしいので、備忘録を残す。

実装方法

### -*- coding utf-8 -*-
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

###
def label_point(x,y,id,ax):
  df_tmp=pd.concat({'x':x,'y':y,'id':id}, axis='columns')
  for i, point in df_tmp.iterrows():
    ax.text(point['x'], point['y'], point['id'],\
    fontsize=5\
    )

###
if __name__=='__main__':
  rfilename='testdata.csv'
  data=pd.read_csv(rfilename)

  # 散布図描画
  ax=sns.scatterplot(x=data.iloc[:,3], y=data.iloc[:,4], hue=data.iloc[:,5])

  # ラベル付け
  label_point(data.iloc[:,3],data.iloc[:,4],data.iloc[:,0],ax)

  # 保存
  wfilename='testdraw.png'
  plt.savefig(wfilename)

データの一部

旅行作文の言語特徴を2次元に落としたものだが、詳しくは割愛する。

id sex age 1-dim 2-dim lab
1 1 63 147.05500793457 -81.8567810058594 me
2 2 45 -128.938018798828 88.1118698120117 fy
3 1 66 20.0744075775146 113.524360656738 me
4 1 68 -64.7453689575195 49.7739143371582 me
5 2 49 -26.7232112884521 -164.791641235352 fy

描画結果

idが浮動小数点になっているが、本題ではないのでこのままで。
testdraw.png

3
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1