【问题标题】:Matplotlib scatter now takes three arguments?Matplotlib scatter 现在需要三个参数?
【发布时间】:2017-09-27 21:27:17
【问题描述】:

我写了一个简短的脚本来显示一些图表:

if __name__=='__main__':
    p1 = gen_spiral(label=0, dt=0,     n_samples=100, )
    p2 = gen_spiral(label=1, dt=np.pi, n_samples=100, )
    print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("p1",   type(p1),   str(np.shape(p1)),   len(p1),    str(p1)))
    print ("Array:   {}\nType:   {}\nShape:   {}\nLength:   {}\nData:   {}\n".format("p2",   type(p2),   str(np.shape(p2)),   len(p2),    str(p2)))


    a = np.arange(1,20,1) 
    b = np.arange(1,20,1) 
    c = np.arange(0.0, 2.0, 0.01)
    d = np.sin(2*np.pi*c)

    fig1 = plt.figure()
    ax1 = fig1.add_subplot(121)
    ax1.scatter(a,b) 
    ax2 = fig1.add_subplot(122)
    ax2.scatter(c,d) 

而且效果很好。但是,当我更改功能以显示我的数据时:

fig1 = plt.figure()
ax1 = fig1.add_subplot(121)
ax1.scatter(p1)

它给了我一个不应该存在的错误:

Traceback (most recent call last):
  File "Theano--PlotSet--ME01.py", line 53, in <module>
    ax1.scatter(p1) 
TypeError: scatter() takes at least 3 arguments (2 given)

这不是真的:Scatter 有 3 个参数,而 p1 有两个部分:

    jason@jason-HP-43299:~/Programs/MachineLearning/SectionOne$ python TheanoPS00.py
    Array:   p1
    Type:   <type 'numpy.ndarray'>
    Shape:   (100, 2)
    Length:   100
    Data:   [[-0.0617  0.0534]
     [ 0.0299  0.0913]
     [ 0.0094  0.157 ]
     [ 0.1057  0.1535]
     [ 0.1412  0.2741]
     [ 0.0851  0.1426]

这里到底发生了什么?

【问题讨论】:

标签: python numpy matplotlib machine-learning theano


【解决方案1】:

您在此处调用的scatter 函数是轴的类方法。它的签名是
scatter(self, x, y, *args, **kwargs),有 3 个位置参数,类参数 self 是第一个,您可以通过将其作为轴实例的方法调用它来隐式提供。

知道这一点可以将错误消息转换为“scatter() 至少需要 2 个用户指定的参数(1 个给定)”。这是有道理的,因为 scatter(p1) 使用 1 个参数。

您需要做的是将您的数组拆分为xy 数组,

ax1.scatter(p1[:,0], p1[:,1])

【讨论】:

    【解决方案2】:

    如果p1 有两个部分,您需要按照the spec 的要求将每个部分作为单独的位置参数传递

    您看到的这个TypeError 严格查看源代码中定义的参数数量,并将其与您对函数的使用进行比较。

    大概你想要shape的x和y,所以你可以这样做:

    ax1.scatter(p1.shape[0], p1.shape[1])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-15
      • 2018-06-24
      • 1970-01-01
      • 1970-01-01
      • 2018-06-09
      • 2013-07-29
      • 1970-01-01
      • 2013-04-24
      相关资源
      最近更新 更多