gpt4 book ai didi

python - 使用 seaborn 在 x y 散点图中添加标签

转载 作者:太空狗 更新时间:2023-10-29 17:40:11 26 4
gpt4 key购买 nike

我花了几个小时尝试完成我认为是一项简单的任务,即在使用 seaborn 时将标签添加到 XY 图上。

这是我的代码

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 的示例。

有什么想法吗?谢谢。

最佳答案

一种方法如下:

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

关于python - 使用 seaborn 在 x y 散点图中添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46027653/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com