我正在尝试向lmplot中的每个数据点添加标签。我想用索引标记每个数据点。现在,我的代码如下:
map()
这会将所有标签绘制到最后一个图上。
我尝试使用p1=sns.lmplot(x="target", y="source", col="color", hue="color",
data=ddf, col_wrap=2, ci=None, palette="muted",
scatter_kws={"s": 50, "alpha": 1})
def label_point(x, y, val, ax):
a = pd.concat({'x': x, 'y': y, 'val': val}, axis=1)
for i, point in a.iterrows():
ax.text(point['x']+.02, point['y'], str(point['val']))
label_point(ddf.target, ddf.source, ddf.chip, plt.gca())
而不是整个图形而不是当前的坐标轴,但是会引发错误。
label_point(ddf.target, ddf.source, ddf.chip, plt.gcf())
答案 0 :(得分:0)
问题是,如果将整个数据集传递给标签功能,标注函数应该如何知道要标注哪个图?!
作为示例,您可以使用熊猫的.groupby
遍历唯一的颜色,并为每个颜色创建一个seaborn.regplot
。这样就很容易分别标记每个轴。
import matplotlib.pyplot as plt
import numpy as np; np.random.seed(42)
import pandas as pd
import seaborn as sns
def label_point(df, ax):
for i, point in df.iterrows():
ax.annotate("{:.1f}".format(point['val']), xy = (point['x'], point['y']),
xytext=(2,-2), textcoords="offset points")
df = pd.DataFrame({"x": np.sort(np.random.rand(50)),
"y": np.cumsum(np.random.randn(50)),
"val" : np.random.randint(10,31, size=50),
"color" : np.random.randint(0,3,size=50 )})
colors = ["crimson", "indigo", "limegreen"]
fig, axes = plt.subplots(2,2, sharex=True, sharey=True)
for (c, grp), ax in zip(df.groupby("color"), axes.flat):
sns.regplot(x="x", y="y", data=grp, color=colors[c], ax=ax,
scatter_kws={"s": 25, "alpha": 1})
label_point(grp, ax)
axes.flatten()[-1].remove()
plt.show()