我有一个简单的
matplotlib
图表,我想通过一个函数来添加点。但是当我扩展存储x和y值的数组时,尽管这两个数组都有6个值,但我却得到了错误信息RuntimeError: xdata and ydata must be the same length
。import matplotlib.pyplot as plt
import numpy as np
import time
x = np.array([1, 2, 3])
y = np.array([1, 7, 5])
plt.ion()
fig = plt.figure()
ax = fig.add_subplot(111)
line1, = ax.plot(x, y, 'r-')
plt.show()
def update_points(new_x, new_y):
global x, y, fig, line1, ax
time.sleep(2)
x = np.append(x, new_x)
y = np.append(y, new_y)
line1.set_xdata(x)
line1.set_xdata(y)
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
update_points(np.array([4, 5, 6]), np.array([4, 5, 3]))
line1.set_xdata(y)
应该改为line1.set_ydata(y)
。 - a_guest