【问题标题】:Add text annotation to seaborn lmplot向 seaborn lmplot 添加文本注释
【发布时间】:2019-01-15 02:49:28
【问题描述】:

我正在尝试为聚类结果创建 seaborn lmplot,数据示例如下所示:

    ID   CA     IP  clusters
    38  10.3    5.6   1
    59  10.4    6.1   0
    64  10.0    6.6   1
    35  10.6    5.6   1
    54  10.6    5.6   1
    60  10.2    8.2   1

有两个集群(集群 0 和集群 1),我想根据每个散点图上的“ID”列显示“ID”。尝试了seaborn regplot中的添加文本功能,但出现“FacetGrid没有文本功能”的错误。

seaborn 剧情代码:

ax = sns.lmplot('CA', 'IP', 
     data=df_tr, 
     fit_reg=False, 
     hue="clusters",  palette="Set1",
     scatter_kws={"marker": "D", "s": 50})

plt.title('Calcium vs Phosporus')
plt.xlabel('CA')
plt.ylabel('IP')

还有剧情:

【问题讨论】:

    标签: python plot seaborn


    【解决方案1】:

    问题是seaborn.regplot(在您链接到的站点中使用)返回一个 matplotlib 轴对象,它允许您使用 text 函数。但是,seaborn.lmplot 返回一个 FacetGrid。

    因此,您需要获取 Facetgrid 的轴,您可以使用

    fgrid = sns.lmplot(...)
    ax = fgrid.axes[0,0]  # fgrid.axes return an array of all axes in the figure, so we index the array
    

    从这里您可以使用链接中显示的功能

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 2019-06-20
      • 2018-02-28
      • 1970-01-01
      • 2018-07-27
      • 1970-01-01
      • 2021-08-01
      • 2013-04-11
      • 1970-01-01
      相关资源
      最近更新 更多