1
$\begingroup$

Code:

    fig = plt.figure(figsize = (6,6))
    ax = fig.gca()
    auto_price.loc[:, ['price', 'fuel-type', 'aspiration']].boxplot(by = 'fuel-type', 'aspiration', ax = ax)
    ax.set_title('Box plot of price new')
    ax.set_ylabel('Auto Price')
    ax.set_ylim(0.0, 50000.0)
    plt.show()

Error:
  File "<ipython-input-20-0712de0416f5>", line 3
    auto_price.loc[:, ['price', 'fuel-type', 'aspiration']].boxplot(by = 'fuel-type', 'aspiration', ax = ax, yx = yx)
                                                                                     ^
SyntaxError: positional argument follows keyword argument

Background/additional information:

Following code works fine:

    fig = plt.figure(figsize =(5,5))
    ax = fig.gca()
    auto_price.loc[:,['price', 'horsepower']].boxplot(by = 'horsepower', ax = ax)
    ax.set_title('Box plot of price')
    ax.set_ylabel('Auto Price')
    ax.set_ylim(0.0, 50000.0)
$\endgroup$
5
  • $\begingroup$ All lines of code should be indented four spaces. All indents after that will be preserved. This makes Python code, in particular, much easier to read. $\endgroup$ Sep 12, 2018 at 20:29
  • $\begingroup$ You cannot first pass parameters exclusively and then implicitly $\endgroup$
    – Aditya
    Sep 12, 2018 at 20:43
  • $\begingroup$ @Aditya, what do you mean by 'exclusively' and then 'implicitly'? $\endgroup$
    – user633599
    Sep 12, 2018 at 20:53
  • $\begingroup$ Like you are passing by, ax via an = and aspiration like that, the variables need to match their defined order $\endgroup$
    – Aditya
    Sep 12, 2018 at 20:56
  • $\begingroup$ so how should i change the code to make it work? I am not sure i understand how to make the edit. $\endgroup$
    – user633599
    Sep 12, 2018 at 21:50

1 Answer 1

2
$\begingroup$

As the error message suggests, your error is caused by passing positional argument after keyword argument.

Consider a function

def foo(a,b,c):
    return a+b*c

You can call this function by foo(3,4,5) and it returns 23. In this case, all three arguments are positional arguments, and Python understand them by their position: 3 is the first argument, therefore corresponds to a. Similarly 4 for b and 5 for c.

You can also call this function by foo(b=3,a=4,c=5) and it returns 19. Here all three arguments are keyword arguments, in the sense that 3 is identified as for b because of the keyword, instead of its position.

You can mix the two kinds of arguments, but positional arguments must come before keyword argument, e.g. foo(3,c=4,b=5). There is no confusion here: 3 comes without a keyword, so it is interpreted by its position, therefore 3 is for a.

You cannot write keyword argument before position argument. For instance if you call foo(c=3,4,5), Python got confused: I know 3 is for c, but is 4 for a or b? So if you write like this, you get the error message

SyntaxError: positional argument follows keyword argument

Therefore in your code boxplot(by = 'fuel-type', 'aspiration', ax = ax), you passed positional argument 'aspiration' after keyword argument by = 'fuel-type', causing this error.

To fix, just call 'aspiration' by its keyword as well, i.e. KEYWORD='aspiration'.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.