簡體   English   中英

Seaborn lmplot(python)中的標簽點具有多個圖

[英]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())

這會將所有標簽繪制到最后一個圖上。

帶標簽的lmplot

我嘗試使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM