未来警告:将以下变量作为关键字参数传递:x,y

15
我想绘制一个seaborn的regplot。 我的代码:
x=data['Healthy life expectancy']
y=data['max_dead']
sns.regplot(x,y)
plt.show()

然而,这给我带来了未来的警告错误。如何修复这个警告?
FutureWarning: Pass the following variables as keyword args: x, y. From version 0.12, the only valid 
positional argument will be 'data', and passing other arguments without an explicit keyword will 
result in an error or misinterpretation.

此警告信息也包含在本答案中:FutureWarning: 请将以下变量作为关键字参数传递:x。 - Trenton McKinney
1个回答

36

Seaborn 0.12

  • 使用 seaborn 0.12seaborn 0.11 中的 FutureWarning 现在变成了 TypeError
  • 对于 seaborn 绘图函数,只有 data 可以指定为第一个位置参数。所有其他参数必须使用关键字(例如 x=y=)。
    • sns.*plot(data=penguins, x="bill_length_mm", y="bill_depth_mm")sns.*plot(penguins, x="bill_length_mm", y="bill_depth_mm")
    • sns.*plot(data=penguins.bill_length_mm)sns.*plot(penguins.bill_length_mm)
    • 参见 Overview of seaborn plotting functions
  • 一些关于不正确使用 seaborn 的位置和关键字参数的潜在错误:
    • 当没有传递关键字时,TypeError: *plot() takes from 0 to 1 positional arguments but 3 were given 将出现。
      • sns.*plot(penguins, "bill_length_mm", "bill_depth_mm")
    • 当在传递 xy 作为位置参数之后使用 data= 时,TypeError: *plot() got multiple values for argument 'data' 将出现。
      • sns.*plot("bill_length_mm", "bill_depth_mm", data=penguins)
    • 当为 xy 传递位置参数后,跟随一个不是 data 的关键字参数时,TypeError: *plot() takes from 0 to 1 positional arguments but 2 positional arguments (and 1 keyword-only argument) were given 将出现。
      • sns.*plot(penguins.bill_length_mm, penguins.bill_depth_mm, kind="reg")
  • 参见 TypeError: method() takes 1 positional argument but 2 were given 获取一般的 python 解释。
  • Positional argument vs keyword argument

Seaborn 0.11

  • 从技术上讲,这是一个警告而不是错误,可以暂时忽略,如本答案底部所示。
  • 我建议按照警告的提示做,为seaborn.regplot指定xy参数,或者为任何带有此警告的seaborn绘图函数指定xy参数。
  • sns.regplot(x=x, y=y),其中xyregplot的参数,你正在将xy变量传递给它们。
  • 从版本0.12开始,除data之外的任何位置参数都会导致错误误解
    • 对于那些关心向后兼容性的人,编写一个脚本来修复现有代码,或者不要更新到0.12(一旦可用)。
  • xy被用作数据变量名称,因为这是OP中使用的。数据可以分配给任何变量名(例如ab)。
  • 这也适用于FutureWarning: Pass the following variable as a keyword arg: x,它可以由只需要xy的绘图生成,例如:
    • sns.countplot(penguins['sex']),但应该是sns.countplot(x=penguins['sex'])sns.countplot(y=penguins['sex'])
import seaborn as sns
import pandas as pd

penguins = sns.load_dataset('penguins')

x = penguins.culmen_depth_mm  # or bill_depth_mm
y = penguins.culmen_length_mm  # or bill_length_mm

# plot without specifying the x, y parameters
sns.regplot(x, y)

enter image description here

# plot with specifying the x, y parameters
sns.regplot(x=x, y=y)

# or use
sns.regplot(data=penguins, x='bill_depth_mm', y='bill_length_mm')

enter image description here

忽略警告

  • 我不建议使用这个选项。
  • 一旦 seaborn v0.12 可用,这个选项将不再可行。
  • 从版本 0.12 开始,唯一有效的位置参数将是 data,传递其他参数而没有显式关键字将导致错误或误解。
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)

# plot without specifying the x, y parameters
sns.regplot(x, y)

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接