【问题标题】:Seaborn FacetGrid PointPlot Label Data PointsSeaborn FacetGrid PointPlot 标签数据点
【发布时间】:2018-02-01 14:14:11
【问题描述】:

鉴于以下情况:

import seaborn as sns
attend = sns.load_dataset("attention")
sns.set_style("whitegrid", {'axes.grid' : False,'axes.edgecolor':'none'})
g = sns.FacetGrid(attend, col="subject", col_wrap=5,
size=1.5, ylim=(0, 10))
ax = g.map(sns.pointplot, "solutions", "score", scale=.7)

我想在每行上标记单个数据点(用值标签代替点)。在我仅通过 MatPlotLib 创建的另一个情节中,这是这样完成的:

for i, text in enumerate(ind):
    a.annotate(str(y[i])[:-2], xy=(ind[i], y[i]),fontsize=6, color=c, 
                bbox=dict(pad=.9,alpha=1, fc='white',color='none'),va='center', ha='center',weight='bold')

但是,由于没有定义 ind,我不确定这将如何工作。

【问题讨论】:

    标签: python seaborn facet-grid


    【解决方案1】:

    我也不知道ind 是什么。但是,如果目的是用它们的坐标注释点,您可以在映射到 FacetGrid 的函数中使用 ax.annotate,如下所示:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    attend = sns.load_dataset("attention")
    sns.set_style("whitegrid", {'axes.grid' : False,'axes.edgecolor':'none'})
    g = sns.FacetGrid(attend, col="subject", col_wrap=5,
                      size=1.5, ylim=(0, 10))
    
    def f(x,y, **kwargs):
        ax = sns.pointplot(x,y,**kwargs)
        ax.axhline(5, alpha=0.5, color='grey')
        for i in range(len(x)):
            ax.annotate(str(y.values[i]), xy=(x.values[i]-1, y.values[i]),fontsize=8,
                        xytext = (0,10), textcoords="offset points",
                    color=kwargs.get("color","k"), 
                    bbox=dict(pad=.9,alpha=0.2, fc='limegreen',color='none'),
                    va='center', ha='center',weight='bold')
    
    g.map(f, "solutions", "score", scale=.7)
    
    plt.show()
    

    可能需要在注解中使用xy=(i, y.values[i]),具体取决于数据的样子。

    请注意,这也通过将axhline 放入该函数中来回答your previous question

    如果目的是用注释替换点,请使用xytext = (0,0) 或完全忽略该参数;然后还保留bbox=dict(pad=.9,alpha=1, fc='w',color='none') 并在函数调用中使用markers=""

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    attend = sns.load_dataset("attention")
    sns.set_style("whitegrid", {'axes.grid' : False,'axes.edgecolor':'none'})
    g = sns.FacetGrid(attend, col="subject", col_wrap=5,
                      size=1.5, ylim=(0, 10))
    
    def f(x,y, **kwargs):
        ax = sns.pointplot(x,y,**kwargs)
        ax.axhline(5, alpha=0.5, color='grey')
        for i in range(len(x)):
            ax.annotate(str(y.values[i]), xy=(i, y.values[i]),fontsize=8,
                    color=kwargs.get("color","k"), 
                    bbox=dict(pad=.9,alpha=1, fc='w',color='none'),
                    va='center', ha='center',weight='bold')
    
    g.map(f, "solutions", "score", scale=.7, markers="")
    
    plt.show()
    

    【讨论】:

    • 非常感谢。我假设在注释中使用 xy=(i, y.values[i]) 将允许我用实际值替换点,只要我有一个框。
    • 我可能误解了你。更新后的答案可能更符合您的要求。
    猜你喜欢
    • 1970-01-01
    • 2017-12-03
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-03
    相关资源
    最近更新 更多