传递命名参数时,matplotlib不会绘图 [英] 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()
import matplotlib.pyplot as plt
plt.plot([0.05, 0.1, 0.15], [102, 211, 393])
plt.show()
也就是说,无需显式指定 x =
和 y =
,pyplot即可正常工作.但是,这与文档中的行为不同.嗯,文档并未明确使用 x =
或 y =
,但是它给出了 plot
的签名为 plot(x,y)
.因此,应该允许使用命名参数,并产生相同的结果,对吧?
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?
推荐答案
如果您查看plot的源代码,它是这样的:
If you look at the source code for plot , it goes something like this:
def plot(*args, **kwargs):
...
文档中明确指出x,y数组是强制性的,但是如果传递x = [],y = [],则plot函数会将其识别为kwarg的一部分,而不是args的一部分.Plot 需要两个数组作为直接参数,没有它返回一个空对象集.这些链接会告诉您更多关于它们的信息:
The documentation does it make it clear that x,y arrays are mandatory , but if you pass x=[],y=[] , the plot function would recognise them as parts of kwargs and not the args. Plot expects two arrays as direct arguments without which it returns an empty object set. These links would tell you more about them:
https://pythontips.com/2013/08/04/args-and-kwargs-in-python-explained/ https://www.geeksforgeeks.org/args-kwargs-python/
这篇关于传递命名参数时,matplotlib不会绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!