【发布时间】:2018-06-01 01:01:07
【问题描述】:
我有一个简单的matplotlibchart,我想通过函数添加点。似乎当我扩展存储 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)。
标签: python python-2.7 numpy matplotlib