Python matplotlib set_array()正好接受2个参数(给定3个)

Python matplotlib set_array()正好接受2个参数(给定3个),python,animation,matplotlib,Python,Animation,Matplotlib,我正在尝试设置一个动画来实时显示通过GPIB接口接收的一些数据。只要我使用一条直线,即matplotlib的plot()函数,我就可以很好地工作 但是,由于我使用离散数据点,所以我想使用scatter()函数。 这给我带来了以下错误: “set_array()正好接受2个参数(给定3个)” 错误在下面代码中显示的2个位置中显示 def intitalisation(): realtime_data.set_array([0],[0]) ******ERROR HERE******

我正在尝试设置一个动画来实时显示通过GPIB接口接收的一些数据。只要我使用一条直线,即matplotlib的plot()函数,我就可以很好地工作

但是,由于我使用离散数据点,所以我想使用scatter()函数。 这给我带来了以下错误: “set_array()正好接受2个参数(给定3个)”

错误在下面代码中显示的2个位置中显示

def intitalisation():
    realtime_data.set_array([0],[0])     ******ERROR HERE*******
    return realtime_data,


def update(current_x_data,new_xdata,current_y_data, new_ydata):#

    current_x_data = numpy.append(current_x_data, new_xdata)
    current_y_data =  numpy.append(current_y_data, new_ydata)

    realtime_data.set_array( current_x_data  , current_y_data  )      ******ERROR HERE*******


def animate(i,current_x_data,current_y_data):

    update(current_x_data,new_time,current_y_data,averaged_voltage)
    return realtime_data,


animation = animation.FuncAnimation(figure, animate, init_func=intitalisation, frames = number_of_measurements, interval=time_between_measurements*60*1000, blit=False, fargs= (current_x_data,current_y_data))

figure = matplotlib.pyplot.figure()


axes = matplotlib.pyplot.axes()

realtime_data = matplotlib.pyplot.scatter([],[]) 

matplotlib.pyplot.show()
所以我想问大家的问题是,为什么set_array()认为我要给它传递3个参数?我不明白,因为我只能看到两个论点。 我怎样才能纠正这个错误呢


编辑:我应该注意,显示的代码不完整,只是有错误的部分,为了清楚起见,删除了其他部分。

我认为您对一些事情有点困惑

  • 如果你试图抓住x&y位置的机会,你使用了错误的方法<代码>设置数组控制颜色数组。对于
    scatter
    返回的集合,可以使用
    set_offset
    控制x&y位置。(您使用哪种方法取决于所讨论的艺术家类型。)
  • 这两个参数与三个参数的比较是因为
    artist.set\u array
    是一个对象的方法,所以第一个参数就是所讨论的对象 为了解释第一点,这里有一个简单的暴力动画:

    import matplotlib.pyplot as plt
    import numpy as np
    
    x, y, z = np.random.random((3, 100))
    
    plt.ion()
    
    fig, ax = plt.subplots()
    scat = ax.scatter(x, y, c=z, s=200)
    
    for _ in range(20):
        # Change the colors...
        scat.set_array(np.random.random(100))
        # Change the x,y positions. This expects a _single_ 2xN, 2D array
        scat.set_offsets(np.random.random((2,100)))
        fig.canvas.draw()
    
    为了解释第二点,在python中定义类时,第一个参数是该类的实例(通常称为
    self
    )。每当您调用对象的方法时,都会在后台传递此消息

    例如:

    class Foo:
        def __init__(self):
            self.x = 'Hi'
    
        def sayhi(self, something):
            print self.x, something
    
    f = Foo() # Note that we didn't define an argument, but `self` will be passed in
    f.sayhi('blah') # This will print "Hi blah"
    
    # This will raise: TypeError: bar() takes exactly 2 arguments (3 given)
    f.sayhi('foo', 'bar') 
    

    谢谢Joe,这似乎对我的代码正常工作很有帮助。虽然我不能实际使用set_偏移量,但我已经查看了它的文档,我不明白如何向它发送x和y数据。从理解的角度来看,如果我诚实的话,我从来没有理解过类是什么,它们是用来做什么的,所以将类的实例作为参数传递给它自己的概念实际上没有任何意义。谢谢你的帮助,但我想我要放弃我在这里试图做的事情。@user3389255-如果你分别有x和y的序列,只需传入如下内容:
    artist.set_offset([[x0,x1,],[y0,y1,…])
    。它将转换为2D numpy阵列。在您的示例代码中,它可能是
    实时数据。设置偏移量([current\u x\u data,current\u y\u data])
    。确定。这就给我留下了改变轴的问题。因为在动画中重新绘制图形时,轴不会自动更新。即使在动画函数中使用blit=False,它仍然不会重新绘制。
    shape(offset)=(4097,2)
    shape(alpha)=(4097,)
    s.set_offset(offset)
    似乎可以工作,但是
    s.set_数组(alpha)
    在makeMappingArray
    if len(shape)2和形状[1]!=3:
    我不知道这是什么意思。当我尝试你的例子,当我尝试一个4元素数组时,它起作用了。。。