Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib does not plot when named arguments are passed

Can someone explain this behaviour?

import matplotlib.pyplot as plt
plt.plot(x=[0.05, 0.1, 0.15], y=[102, 211, 393])
plt.show()

No output!

import matplotlib.pyplot as plt
plt.plot([0.05, 0.1, 0.15], [102, 211, 393])
plt.show()

With output!

That is, without specifying x= and y= explicitly, pyplot works fine. However, this is different from the behaviour in the documentation. Well, the documentation does not explicitly use x= or y=, but it gives the signature of plot as plot(x, y). Therefore, named arguments should be permitted, and produce the same result, right?

like image 422
Kartik Avatar asked Aug 12 '26 06:08

Kartik


1 Answers

I think the confusion comes from the difference in signatures. This is a python problem.

Consider

def func1(x,y):
    print(x,y)

def func2(*args):
    print(args)

You may call each of them with positional arguments.

func1(1,2)
func2(1,2)

You may also call the first function with named arguments.

func1(x=1, y=2)

However, you cannot call the second function with named arguments, because x and y are not defined.

func2(x=1, y=2)

results in an Error.

As seen from the documentation of plot, it uses the second way of defining the function,

matplotlib.pyplot.plot(*args, **kwargs)

I think the matplotlib documentation needs to assume some basic python understanding, but if there is something that can be done to clarify it, feel free to reach out with a suggestion on what to improve.

like image 190
ImportanceOfBeingErnest Avatar answered Aug 13 '26 21:08

ImportanceOfBeingErnest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!