鉴于以下情况:
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()
可能,您需要在注释中使用 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()
关于python - Seaborn FacetGrid PointPlot 标签数据点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45849028/