如何在 Seaborn 中传递位置和关键字参数

问题描述 投票:0回答:1

我想绘制一个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.
python seaborn typeerror keyword-argument positional-argument
1个回答
36
投票

Seaborn 0.12

  • 使用
    seaborn 0.12
    FutureWarning
    中的
    seaborn 0.11
    现在是
    TypeError
  • 只能将
    data
    指定为seaborn图的第一个位置参数。所有其他参数必须使用关键字(例如
    x=
    y=
    )。
    这适用于所有
    seaborn
    绘图函数。
    • 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)
    • 参见 seaborn 绘图函数概述
  • seaborn 中错误使用位置和关键字参数的一些潜在错误:
      当没有传递关键字时,会出现
    • TypeError: *plot() takes from 0 to 1 positional arguments but 3 were given
      • sns.*plot(penguins, "bill_length_mm", "bill_depth_mm")
    • 当将
    • TypeError: *plot() got multiple values for argument 'data'
      data=
       作为位置参数传递后使用 
      x
       时,会发生 
      y
      • sns.*plot("bill_length_mm", "bill_depth_mm", data=penguins)
    • 当为
    • TypeError: *plot() takes from 0 to 1 positional arguments but 2 positional arguments (and 1 keyword-only argument) were given
      x
       传递位置参数,后跟除 
      y
       之外的关键字参数时,会出现 
      data
      • sns.*plot(penguins.bill_length_mm, penguins.bill_depth_mm, kind="reg")
  • 请参阅 TypeError:method() 采用 1 个位置参数,但给出了 2 个以获得一般
    python
    解释。
  • 位置参数与关键字参数

Seaborn 0.11

  • 从技术上讲,这是一个警告,而不是错误,现在可以忽略,如本答案的底部所示。
  • 我建议按照警告所述操作,为
    x
    或任何其他 
    seaborn 绘图函数
    指定
    yseaborn.regplot 参数。
  • sns.regplot(x=x, y=y)
    ,其中
    x
    y
    regplot
    的参数,您要向其传递
    x
    y
    变量。
  • 从版本 0.12 开始,传递任何 位置参数
    data
    除外)将导致
    error
    misinterpretation
    • 对于那些关心向后兼容性的人,请编写一个脚本来修复现有代码,或者不要更新到 0.12(一旦可用)。
  • x
    y
    用作数据变量名称,因为这是 OP 中使用的名称。数据可以分配给任何变量名称(例如
    a
    b
    )。
  • 这也适用于
    FutureWarning: Pass the following variable as a keyword arg: x
    ,它可以通过仅需要
    x
    y
    的绘图生成,例如:
    • 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)

# 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')

忽略警告

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

© www.soinside.com 2019 - 2024. All rights reserved.