Skip to content Skip to sidebar Skip to footer

Python Matplotlib: Dynamically Update Plot - Array Length Not Known A Priori

I am aware of these questions: (A), (B) and (C) - all of which address parts of my problem. I have also read the Animations Cookbook My questions, however, seem not to be addressed

Solution 1:

Just make the line objects with empty data outside of your loop:

line1, = ax.plot([], [],'-k',label='black')
line2, = ax.plot([], [],'-r',label='red')
ax.legend()
for i in range(0, SIZE):
  A.append(R1 * i * np.sin(i))
  B.append(R2 * i * np.cos(i))
  line1.set_ydata(A)
  line1.set_xdata(range(len(A)))
  line2.set_ydata(B)
  line2.set_xdata(range(len(B)))
  ax.relim()
  ax.autoscale_view()
  plt.draw() 

You can probably be a bit more clever about updating your xdata.

For a more complete example see here and the full gallery of animation examples.

Post a Comment for "Python Matplotlib: Dynamically Update Plot - Array Length Not Known A Priori"