【问题标题】:Annotate text in facetgrid of sns relplot in python在python中sns relplot的facetgrid中注释文本
【发布时间】:2021-02-24 00:12:03
【问题描述】:

使用以下数据框(实用程序):

    Security_Name          Rating    Duracion   Spread
0   COLBUN 3.95 10/11/27    BBB     6.135749    132
1   ENELGX 4 1/4 04/15/24   BBB+    3.197206    124
2   PROMIG 3 3/4 10/16/29   BBB-    7.628048    243
3   IENOVA 4 3/4 01/15/51   BBB     15.911632   364
4   KALLPA 4 7/8 05/24/26   BBB-    4.792474    241
5   TGPERU 4 1/4 04/30/28   BBB+    4.935607    130

数据框

我正在尝试创建一个 sns relplot,它应该注释相应 facetgrid 中的散点图点。但是我得到的输出看起来像这样(没有注释)

关系图

我在任何情节中都看不到任何注释

我尝试了以下代码:

sns.relplot(x="Duracion", y="Spread", col="Rating", data=utilities)

我真的不知道从哪里开始使用 facetrgid 为这个重新绘制添加注释。注释应该是 Security_Name 列的值

请告知修改。提前致谢。

【问题讨论】:

    标签: pandas matplotlib annotations seaborn facet-grid


    【解决方案1】:

    使用FacetGrid 和自定义注解函数,可以得到想要的结果。请注意,鉴于提供的示例数据框,注释很有可能会重叠:

    def annotate_points(x,y,t, **kwargs):
        ax = plt.gca()
        data = kwargs.pop('data')
        for i,row in data.iterrows():
            ax.annotate(row[t], xy=(row[x],row[y]))
            
    g = sns.FacetGrid(col="Rating", data=df)
    g.map(sns.scatterplot, "Duracion", "Spread")
    g.map_dataframe(annotate_points, "Duracion", "Spread", 'Security_Name')
    

    【讨论】:

      猜你喜欢
      • 2020-05-03
      • 2022-01-11
      • 1970-01-01
      • 2022-10-12
      • 2019-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多