0

I am trying to make a point move on the random plots in matplotlib. Please refer to the following code.

import numpy as np 
import matplotlib.pyplot as plt 
from time import sleep
plt.ion()
a = np.array([[1,1],[5,5],[2,4],[7,8],[4,2],[10,11]])
b = []
for i in range(a.shape[0]/2):
    b.append([a[2*i],a[2*i+1]])
b = np.array(b)

for i in b:    # retain this plot
    plt.plot(i[:,0],i[:,1])

for i in b:
    t = np.linspace(0,1,100)
    for k in t:
        x = (1-k)*i[0,0]+k*i[1,0]
        y = (1-k)*i[0,1]+k*i[1,1]
        sleep(0.1)
        # plt.gcf().clear()
        plt.scatter([x],[y])
        plt.draw()

When I run this code, a point is moving on the plots but it is leaving traces. But, I want a single point move without leaving traces and retain the plot I made before plotting the point. If I use clear() (I commented it in the code), it is clearing the whole plot and plotting only the point.

I tried to use FuncAnimation(), but I am finding difficulty in plotting all the other plots whose number is more than 300.

Is there any method I can use. Plase, help.

Thanks

rsvar67
  • 77
  • 8
  • You're plotting a new dot with plt.scatter([x],[y]) at each loop step, not modifying the dot properties. – hyamanieu Jul 25 '17 at 09:19
  • @Wli I did not understand "modifying the dot properties" part. Could you please elaborate a bit. – rsvar67 Jul 25 '17 at 11:33
  • check the duplicate. – hyamanieu Jul 25 '17 at 12:09
  • I checked the other question. In a way, it is not duplicate as it does not have a background plot on which a point moves. I want to have a background plot with hundreds of lines (only three lines are shown in the sample code). – rsvar67 Jul 26 '17 at 03:22
  • You can inspire yourself from the answer. The OP posted a similar code than yours. – hyamanieu Jul 26 '17 at 08:27

0 Answers0