在Seaborn的x y散点图中添加标签 [英] Adding labels in x y scatter plot with seaborn
本文介绍了在Seaborn的x y散点图中添加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我花了数小时试图做我认为简单的任务,即在使用seaborn时将标签添加到XY图上.
I've spent hours on trying to do what I thought was a simple task, which is to add labels onto an XY plot while using seaborn.
这是我的代码
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')
我想将物种"(Species)列中的文本添加到图中的每个点.
I would like to add to each dot on the plot the text in "species" column.
我已经看到许多使用matplotlib的示例,但没有使用seaborn的示例.
I've seen many examples using matplotlib but not using seaborn.
有什么想法吗?谢谢.
推荐答案
一种方法如下:
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())
这篇关于在Seaborn的x y散点图中添加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文