为什么不能在 pyplot 中将参数显式传递为 x 和 y?

Sri*_*vas 11 python matplotlib

我正在使用作为 plt 导入的 matplotlib.pyplot 模块进行绘图。

在 plt.plot() 语句中,如果我将参数传递为“x= array1,”y= array2”,则会收到“TypeError:plot 获得意外的关键字参数 'x' ”。

如果我简单地传递“array1 和 array2”,而没有明确说明它们对应于 x 轴和 y 轴,则代码会正确执行。

这是为什么?

Gij*_*ijs 5

如果您查看函数定义,https://github.com/matplotlib/matplotlib/blob/9a24fb724331f50baf0da4d17188860357d328a9/lib/matplotlib/axes/_axes.py#L72,您可以看到那里的星号,并且它的使用不会不能使用关键字作为非可选参数。例如,请参阅Python args 和 kwargs:揭秘。

def plot(self, *args, scalex=True, scaley=True, data=None, **kwargs):
        """
        Plot y versus x as lines and/or markers.
        Call signatures::
            plot([x], y, [fmt], *, data=None, **kwargs)
            plot([x], y, [fmt], [x2], y2, [fmt2], ..., **kwargs)
Run Code Online (Sandbox Code Playgroud)