1
간단한 matplotlib
차트를 통해 함수를 통해 포인트를 추가하고 싶습니다. 배열을 확장하여 x 및 y 값을 저장하면 두 배열이 모두 6 개의 값을 가지지 만 RuntimeError: xdata and ydata must be the same length
오류가 발생합니다.Matplotlib set_data xdata와 ydata의 길이가 같아야합니다.
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]))
감사합니다. 이것은 작동하지만 여전히'plt.draw() '대신에'fig.canvas.draw()'가 필요합니다. – user2242044
네, 왜냐하면 여기서 처음으로 드로잉을하기 때문입니다. 나는 '만약 당신이 이미 그것을 그린다면, 다시 그리기를 원할 것입니다. –
그래서 여러 번 호출 할 경우 이미 그려져있는 줄을 확인하고 두 번째 호출 대신'plt.draw()를 호출하는 방법이 있습니까? – user2242044