LoginSignup
31
23

More than 5 years have passed since last update.

Pandasで散布図を書くとき各要素のラベルを表示

Last updated at Posted at 2016-10-13

Pandasで散布図を書くときのメモ

Pandasで散布図(scatter)を書く方法は、結構簡単に見つかります。でも、以下のように、各要素にラベルを表示させる方法がなかなか見当たらなかったのでメモしておきます。

scatter-simple-text.png

Pandasのiterrows()で各行をforで繰り返し、aanotateで描画するまで。

scatter-label.py
import pandas as pd

# DataFrameにデータをセット
dd = pd.DataFrame([
        [10,50,'hoge'],
        [50,30,'fpp'],
        [20,30,'baa']
    ], columns=['ax','ay','label'])

# 散布図を描画
a = dd.plot.scatter(x='ax',y='ay')
# 各要素にラベルを表示
for k, v in dd.iterrows():
    a.annotate(v[2], xy=(v[0],v[1]), size=15)
31
23
0

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
31
23