[英]Label Points in Seaborn lmplot (python) with multiple plots
我正在嘗試向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, 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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.