python - Seaborn FacetGrid PointPlot 标签数据点

标签 python seaborn facet-grid

鉴于以下情况:

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

最佳答案

我也不知道 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()

enter image description here

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

请注意,这也回答了 your previous question通过将 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()

enter image description here

关于python - Seaborn FacetGrid PointPlot 标签数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45849028/

相关文章:

python - Seaborn 热图中的自定义调色板

r - 如何在函数 ggplot 中添加子图?

javascript - 如何将 jQuery 变量设置为 django 模板变量

python - 在 Django 中搜索应用程序

python - 寻找方法/模块以确定用于构建 CPython 解释器的编译器

python - 安装具有来自本地源代码分发的 setup_requires 的包

python - 并排绘制两个seaborn热图

python - 如何创建显示均值、中位数和众数的seaborn fiddle 图?

python - 如何避免 seaborn FacetGrid 中的空网格

r - 我如何使用 geom_rect 在夜间添加阴影而不扩展刻面比例?