6

鉴于以下情况:

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,我不确定这将如何工作。

4

1 回答 1

9

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

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

如果目的是用注释替换点,则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()

在此处输入图像描述

于 2017-08-23T21:49:27.623 回答