如何修复Seaborn中lmplot()的TypeError:参数'data'被多次赋值
修复Seaborn lmplot的TypeError错误
问题场景
使用Seaborn绘制特征与标签的散点图时,运行以下代码出现错误:
df=pd.read_csv("breast-cancer.data.txt",na_values=['?']) df.drop(["id"],axis=1,inplace=True) df["label"].replace(2,0,inplace=True) df["label"].replace(4,1,inplace=True) df.dropna(inplace=True) full_data=df.astype(float).values.tolist() df.head() for i in range(1,10): sns.lmplot(str('x'+str(i)), 'label', data=df, fit_reg=False)
错误信息:
TypeError Traceback (most recent call last) Cell In [6], line 2 1 for i in range(1,10): ----> 2 sns.lmplot(str('x'+str(i)), 'label', data=df, fit_reg=False) TypeError: lmplot() got multiple values for argument 'data'
修复方案
错误原因是新版本Seaborn(v0.11+)的lmplot不再支持通过位置参数传递x和y,必须显式用关键字参数指定x、y参数。
修改后的代码:
df=pd.read_csv("breast-cancer.data.txt",na_values=['?']) df.drop(["id"],axis=1,inplace=True) df["label"].replace(2,0,inplace=True) df["label"].replace(4,1,inplace=True) df.dropna(inplace=True) full_data=df.astype(float).values.tolist() df.head() for i in range(1,10): # 显式指定x和y的关键字参数 sns.lmplot(x=str('x'+str(i)), y='label', data=df, fit_reg=False)
补充说明
fit_reg=False参数是正确的,用来关闭线性拟合线,只保留散点图,符合需求。修改后代码会遍历每个x特征,绘制与label的散点图。
内容的提问来源于stack exchange,提问作者Soomin Im
相关产品推荐
相关产品推荐

