我正在尝试设置动画以实时显示通过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()
Run Code Online (Sandbox Code Playgroud)
所以我的问题是,为什么set_array()认为我传递3个参数呢?我不明白,因为我只能看到两个论点.我该如何纠正这个错误?
编辑:我应该注意,显示的代码不完整,只是有错误的部分,为清楚起见删除了其他部分.
我觉得你在几件事情上有点困惑.
set_array控制颜色数组.对于scatter返回的集合,您将使用它set_offsets来控制x和y位置.(您使用哪种方法取决于相关艺术家的类型.)artist.set_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()
Run Code Online (Sandbox Code Playgroud)
为了解释第二点,当你在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')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8383 次 |
| 最近记录: |