簡體   English   中英

Seaborn regplot:如何截斷回歸線和 CI?

[英]Seaborn regplot: how to truncate regression line and CI?

我有一個散點圖,其中數據的下限為零(兩個軸)。 但是,當使用 lmplot 繪制回歸線時,我在 y 軸中得到該線和 CI 的負值。 有沒有辦法在 y 軸上綁定 regline 和 CI?

使用的代碼:

ax1 = sns.lmplot(x=x, y=y, hue=z, data=df, fit_reg=False, 
             scatter_kws={'s':70}, height=7, aspect=1.2, 
             legend=False, palette=colors)
sns.regplot(x=x, y=y, data=df, scatter=False, ax=ax1.axes[0, 0], color='grey')
ax1.set(ylim=(-2, None))

在此處輸入圖片說明

您可以使用大矩形剪裁線和置信區間。

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

N = 80
xs = np.random.uniform(1, 10, N)
ys = 20 / xs + 2*np.random.uniform(0, 1, N)**4
df = pd.DataFrame({'x': xs, 'y': ys, 'z': np.random.randint(0, 2, N)})
g = sns.lmplot(x='x', y='y', hue='z', data=df, fit_reg=False,
               scatter_kws={'s': 70}, height=7, aspect=1.2,
               legend=False, palette='turbo')
ax = g.axes[0, 0]
sns.regplot(x='x', y='y', data=df, scatter=False, ax=ax, color='grey')
rect = plt.Rectangle((-1000, 0), 2000, 2000, color='r', alpha=0.2)
ax.add_patch(rect) # the clipping rectangle needs to be added to the ax to receive the correct transform
ax.collections[-1].set_clip_path(rect)
ax.lines[-1].set_clip_path(rect)
rect.remove() # remove the rectangle again
ax.relim() # recalculating the limits after removing the rectangle
ax.set(ylim=(-1, None))
plt.show()

sns.regplot 與剪切回歸線和 CI

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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