gpt4 book ai didi

python - 使用 pandas 数据框标记 matplotlib.pyplot.scatter

转载 作者:行者123 更新时间:2023-12-03 08:50:13 27 4
gpt4 key购买 nike

我有一个 pandas 数据框,我想将其作为标签应用到散点图上的每个点。就数据而言,它是聚类数据,数据帧包含每个点的标签以及它属于哪个簇。将其投影到上面的散点图上会很有帮助。我尝试使用注释并出现错误。下面是我的散点图代码:

 import hdbscan
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import umap
from sklearn.decomposition import PCA
import sklearn.cluster as cluster
from sklearn.metrics import adjusted_rand_score,
adjusted_mutual_info_score

se1= umap.UMAP(n_neighbors = 20,random_state=42).fit_transform(data_1)

cluster_1 = hdbscan.HDBSCAN(min_cluster_size = 15, min_samples =3).fit_predict(se1)
clustered = (cluster_1 >=0)
plt.scatter(se1[~clustered,0],se1[~clustered,1],c=(0.5,0.5,0.5), s=5, alpha =0.5)
plt.scatter(se1[clustered,0], se1[clustered,1], c=cluster_1[clustered],s=5, cmap='prism');
plt.show()

enter image description here

如何将 df1(960 行 x 1 列)作为标签添加到上面散点图中的所有点?

  df1 = pd.DataFrame(cluster_1)
plt.annotate(cluster_3,se3[clustered,0], se3[clustered,1])

*错误:“回溯(最近一次调用最后一次): 文件“”,第 1 行,位于 文件“C:\Users\trivedd\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\pyplot.py”,第 2388 行,注释中 返回 gca().annotate(s, xy, *args, **kwargs) 文件“C:\Users\trivedd\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\axes_axes.py”,第 791 行,注释中 a = mtext.Annotation(s, xy, *args, **kwargs) 文件“C:\Users\trivedd\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\cbook\deprecation.py”,第 307 行,包装器中 返回 func(*args, **kwargs) 文件“C:\Users\trivedd\AppData\Local\Continuum\anaconda3\lib\site-packages\matplotlib\text.py”,第 2166 行,在 init 中 x, y = xy 文本ValueError:需要解压的值太多(预计为 2)”*

最佳答案

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import string
%matplotlib inline
df = pd.DataFrame({'x':np.random.rand(10),'y':np.random.rand(10),'label':list(string.ascii_lowercase[:10])})

df 看起来像这样

x   y   label
0.854133 0.020296 a
0.320214 0.857453 b
0.470433 0.103763 c
0.698247 0.869477 d
0.366012 0.127051 e
0.769241 0.767591 f
0.219338 0.351735 g
0.882301 0.311616 h
0.083092 0.159695 i
0.403883 0.460098 j

尝试:

ax = df.plot(x='x',y='y',kind='scatter',figsize=(10,10))
df[['x','y','label']].apply(lambda x: ax.text(*x),axis=1)

给你这个:

enter image description here

或者如果您想使用图例:

import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
import numpy as np
import string
%matplotlib inline
df = pd.DataFrame({'x':np.random.rand(50), 'y':np.random.rand(50),'label': [int(x) for x in '12345'*10]})

fig, ax = plt.subplots(figsize=(5,5))
ax = sns.scatterplot(x='x',y='y',hue = 'label',data = df,legend='full',
palette = {1:'red',2:'orange',3:'yellow',4:'green',5:'blue'})
ax.legend(loc='lower left')

enter image description here

关于python - 使用 pandas 数据框标记 matplotlib.pyplot.scatter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59431722/

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