FutureWarning: Pass the following variables as keyword args: x, y

62.8k views Asked by At

I want to plot a seaborn regplot. my code:

x=data['Healthy life expectancy']
y=data['max_dead']
sns.regplot(x,y)
plt.show()

However this gives me future warning error. How to fix this warning?

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.
1

There are 1 answers

0
Trenton McKinney On BEST ANSWER

Seaborn 0.12

  • With seaborn 0.12, the FutureWarning from seaborn 0.11 is now an TypeError.
  • Only data may be specified as the first positional argument for seaborn plots. All other arguments must use keywords (e.g. x= and y=). This applies to all seaborn plotting functions.
    • sns.*plot(data=penguins, x="bill_length_mm", y="bill_depth_mm") or sns.*plot(penguins, x="bill_length_mm", y="bill_depth_mm")
    • sns.*plot(data=penguins.bill_length_mm) or sns.*plot(penguins.bill_length_mm)
    • See Overview of seaborn plotting functions
  • Some potential errors for incorrect use of positional and keyword arguments with seaborn:
    • TypeError: *plot() takes from 0 to 1 positional arguments but 3 were given occurs when no keywords are passed.
      • sns.*plot(penguins, "bill_length_mm", "bill_depth_mm")
    • TypeError: *plot() got multiple values for argument 'data' occurs when data= is used after passing x and y as positional arguments.
      • 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 occurs when positional arguments are passed for x and y, followed by a keyword argument other than data
      • sns.*plot(penguins.bill_length_mm, penguins.bill_depth_mm, kind="reg")
  • See TypeError: method() takes 1 positional argument but 2 were given for the general python explanation.
  • Positional argument vs keyword argument

Seaborn 0.11

  • Technically, it's a warning, not an error, and can be ignored for now, as shown in the bottom section of this answer.
  • I recommend doing as the warning says, specify the x and y parameters for seaborn.regplot, or any of the other seaborn plot functions with this warning.
  • sns.regplot(x=x, y=y), where x and y are parameters for regplot, to which you are passing x and y variables.
  • Beginning in version 0.12, passing any positional arguments, except data, will result in an error or misinterpretation.
    • For those concerned with backward compatibility, write a script to fix existing code, or don't update to 0.12 (once available).
  • x and y are used as the data variable names because that is what is used in the OP. Data can be assigned to any variable name (e.g. a and b).
  • This also applies to FutureWarning: Pass the following variable as a keyword arg: x, which can be generated by plots only requiring x or y, such as:
    • sns.countplot(penguins['sex']), but should be sns.countplot(x=penguins['sex']) or 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

Ignore the warnings

  • I do not advise using this option.
  • Once seaborn v0.12 is available, this option will not be viable.
  • 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.
import warnings
warnings.simplefilter(action="ignore", category=FutureWarning)

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

enter image description here