2017-09-03 9 views
1

シーボンを使用しているときにXYプロットにラベルを追加するのは簡単なことでした。シーボーンを使ったx y散布図のラベルの追加

は、ここに私のコード

私は「種」の欄にプロット上の各ドットにテキストを追加したいと思います
import seaborn as sns 
import matplotlib.pyplot as plt 
%matplotlib inline 

df_iris=sns.load_dataset("iris") 

sns.lmplot('sepal_length', # Horizontal axis 
      'sepal_width', # Vertical axis 
      data=df_iris, # Data source 
      fit_reg=False, # Don't fix a regression line 
      size = 8, 
      aspect =2) # size and dimension 

plt.title('Example Plot') 
# Set x-axis label 
plt.xlabel('Sepal Length') 
# Set y-axis label 
plt.ylabel('Sepal Width') 

です。

私はmatplotlibを使用していますが、seabornを使用していない例がたくさんあります。

アイデア?ありがとうございました。次のようにあなたがこれを行うことができます

+0

あなたは例のデータフレームを提供することはできますか? 'z'はX軸とY軸のラベル情報を含んでいますか?軸全体、または目盛りにラベルを付けたいですか? SeabornはMatplotlibをフードの下で使用しています - あなたはプロットにラベルを付けるためだけに 'plt'メソッドを使用したくないと言っていますか? –

+0

サンプルデータセットが追加されました。申し訳ありません –

答えて

4

一つの方法は次のとおりです。

import seaborn as sns 
import matplotlib.pyplot as plt 
import pandas as pd 
%matplotlib inline 

df_iris=sns.load_dataset("iris") 

ax = sns.lmplot('sepal_length', # Horizontal axis 
      'sepal_width', # Vertical axis 
      data=df_iris, # Data source 
      fit_reg=False, # Don't fix a regression line 
      size = 10, 
      aspect =2) # size and dimension 

plt.title('Example Plot') 
# Set x-axis label 
plt.xlabel('Sepal Length') 
# Set y-axis label 
plt.ylabel('Sepal Width') 


def label_point(x, y, val, ax): 
    a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1) 
    for i, point in a.iterrows(): 
     ax.text(point['x']+.02, point['y'], str(point['val'])) 

label_point(df_iris.sepal_length, df_iris.sepal_width, df_iris.species, plt.gca()) 

enter image description here

+0

Scottありがとうございます。それはプロットしますが、私にとってはプロットされた文字列が奇妙に見えます。それぞれのポイントは、次の通りです。「種:setosa、Name:3、dtype:object」どのように修正するか? –

関連する問題