【问题标题】:seaborn FutureWarning: Pass the following variables as keyword args: x, yseaborn FutureWarning:将以下变量作为关键字参数传递:x,y
【发布时间】:2021-01-15 16:45:01
【问题描述】:

我想绘制一个 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.

【问题讨论】:

  • 接受的答案适用于任何有此问题的seaborn plots。不要为每种情节添加单独的答案,它将被删除为duplicate,其中已经有happened
  • FutureWarning: Pass the following variable as a keyword arg: x 也包含在此答案中。

标签: python plot seaborn


【解决方案1】:
  • 从技术上讲,这是一个警告,而不是错误,现在可以忽略,如本答案的底部所示。
  • 我建议按照警告中的说明进行操作,为 seaborn.regplot 指定 xy 参数,或任何其他 seaborn plot functions 出现此警告。
  • sns.regplot(x=x, y=y),其中xyregplot 的参数,您将向其传递xy 变量。
  • 从版本 0.12 开始,传递任何 positional argumentsdata 除外)将导致 errormisinterpretation
    • 对于那些关心向后兼容性的人,编写一个脚本来修复现有代码,或者不要更新到 0.12(一旦可用)。
  • xy 用作数据变量名称,因为这就是 OP 中使用的名称。数据可以分配给任何变量名(例如ab)。
  • 这也适用于FutureWarning: Pass the following variable as a keyword arg: x,它可以由只需要xy的地块生成,例如:
    • sns.countplot(pen['sex']),但应该是 sns.countplot(x=pen['sex'])sns.countplot(y=pen['sex'])
import seaborn as sns
import pandas as pd

pen = sns.load_dataset('penguins')

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

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

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

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

忽略警告

  • 我不建议使用此选项。
  • 一旦 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)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-24
    相关资源
    最近更新 更多