Seaborn lmplot(python)中具有多个绘图的标签点
我正在尝试向lmplot中的每个数据点添加标签。我想用索引标记每个数据点。现在我的代码如下:Seaborn lmplot(python)中具有多个绘图的标签点,python,seaborn,Python,Seaborn,我正在尝试向lmplot中的每个数据点添加标签。我想用索引标记每个数据点。现在我的代码如下: 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,
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())
而不是使用整个图形而不是当前轴,但它抛出了一个错误
ValueError: Image size of 163205x147206 pixels is too large.
It must be less than 2^16 in each direction.
问题是,如果将整个数据集传递给标签函数,标签函数如何知道要标记哪个绘图 例如,您可以使用pandas的
.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()