0

I have written a code that plot some points and lines on the xy plane. It plots everything for a given value of n. So for different n I get my desired plots. But I want to animate these plots for different values of n, say, for n=1, 2, ..., 100. But I cannot do this animation.

Can anyone here help me to do this? Thank you.. I paste my code here:

My Code

import matplotlib as mpl
mpl.rc('text', usetex = True) 
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle

fig = plt.subplots()
ax = plt.axes(xlim=(-1.2, 1.2), ylim=(-1.2, 1.2))

plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot','dark_background'])
plt.rcParams['figure.figsize'] = (12, 8)

n = 10 #I want to animate this n.
p = 2

for k in range(0,n,1):
    theta1 = np.pi + 2*k*np.pi / n
    theta2 = np.pi + 2*p*k*np.pi / n
    x, y = np.cos(theta1), np.sin(theta1)
    x1, y1 = np.cos(theta2), np.sin(theta2)
    plt.scatter(x, y, s=50, c='violet', zorder=3)
    plt.plot([x,x1], [y,y1], color = 'w')

circle = plt.Circle((0, 0), 1, color='c', fill=False, lw = 1)
ax.add_artist(circle)

#Customize the axes and gridlines:
ax.grid(False)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.spines['bottom'].set_visible(False)
#TickMarks Customization:
ax.set_xticks([])
ax.set_yticks([])

#plt.savefig('nthRoots.png', format='png', dpi=1000,bbox_inches='tight')

plt.show()

Output Output

Is it possible to animate n over different values?

EDIT: Here I donot have only scatter plots ...so I cannot understand how to do this job using those links..!

My Attempt

#Animation.
import matplotlib as mpl
mpl.rc('text', usetex = True) #for LaTex notation in the Plot
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
from matplotlib import animation, rc

rc('animation', html='html5')

fig = plt.subplots()
ax = plt.axes(xlim=(-1.2, 1.2), ylim=(-1.2, 1.2))
plt.gca().set_aspect('equal', adjustable='box')
plt.style.use(['ggplot','dark_background'])
plt.rcParams['figure.figsize'] = (12, 8)

p = 2

#Plotting Function:
def f(n):
    for k in range(0,n,1):
        theta1 = np.pi + 2*k*np.pi / n
        theta2 = np.pi + 2*p*k*np.pi / n
        x, y = np.cos(theta1), np.sin(theta1)
        x1, y1 = np.cos(theta2), np.sin(theta2)
        plt.scatter(x, y, s=50, c='violet', zorder=3)
        plt.plot([x,x1], [y,y1], color = 'w')

    circle = plt.Circle((0, 0), 1, color='c', fill=False, lw = 1)
    ax.add_artist(circle)

    #Customize the axes and gridlines:
    ax.grid(False)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    #TickMarks Customization:
    ax.set_xticks([])
    ax.set_yticks([])
    plt.show()

#Now I try to define a function for animating n in f(n)
def animate(n):
    f(n)

anim = animation.FuncAnimation(fig, animate,
                           frames=100, interval=100, blit=True)
#anim.save('Wave.mp4', writer = 'ffmpeg', fps = 2, dpi=500,extra_args=['-vcodec', 'libx264'])

That's all I had... But this idea didn't work...I think I have to properly define animate(n). Any suggestion...! thanks.

sigma
  • 227
  • 3
  • 13
  • Possible duplicate of [How to animate a scatter plot?](https://stackoverflow.com/questions/9401658/how-to-animate-a-scatter-plot) – Sheldore Sep 22 '18 at 10:17
  • Another link [here](https://stackoverflow.com/questions/42722691/python-matplotlib-update-scatter-plot-from-a-function) – Sheldore Sep 22 '18 at 10:17
  • @Bazingaa...Thanks ,,,I understand a **little** your 2nd link ...but its all about scatter plot ....In my case I also need to plots the lines, circle ete,....How can I do this ...? Sry if its seem so silly...I am new to animation.. – sigma Sep 22 '18 at 10:23
  • and I also prefer ```FuncAnimation``` method... – sigma Sep 22 '18 at 10:24
  • I try to do this using your links ...But it doesn't do my job... – sigma Sep 22 '18 at 10:43
  • It's always easier to help if you show what you have tried. So currently there is no `FuncAnimation` in the code. – ImportanceOfBeingErnest Sep 22 '18 at 11:12
  • @ImportanceOfBeingErnest....I try to define a function ```def f(n):``` that takes a value of ```n``` and do all the task as in my code... Then to do the animation I try : ```define animate(n):```....but it didn't work – sigma Sep 22 '18 at 11:20
  • Good. Now what would be helpful is if this question included that code that didn't work, because in principle that is of course the correct strategy. – ImportanceOfBeingErnest Sep 22 '18 at 12:40
  • Okay..I Edit my code now – sigma Sep 22 '18 at 12:42
  • @ImportanceOfBeingErnest...Can you please tell me anything for further progress..!! – sigma Sep 22 '18 at 12:59

1 Answers1

1

Several problems in your code (most are unrelated to animations)

  • rcParams need to be defined before creating the figure
  • plt.subplots returns a tuple of figure and axes.
  • The animation must return a sequence of artist objects when blitting is used. You might turn it off though
  • plt.show() should be called once at the end of the script.

Correcting for those you get

import matplotlib as mpl
mpl.rc('font', family = 'serif')
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Circle
from matplotlib import animation, rc

plt.rcParams['figure.figsize'] = (12, 8)
plt.style.use(['ggplot','dark_background'])

fig, ax = plt.subplots()


p = 2

#Plotting Function:
def f(n):
    ax.clear()
    ax.set(xlim=(-1.2, 1.2), ylim=(-1.2, 1.2))
    ax.set_aspect('equal', adjustable='box')

    for k in range(0,n,1):
        theta1 = np.pi + 2*k*np.pi / n
        theta2 = np.pi + 2*p*k*np.pi / n
        x, y = np.cos(theta1), np.sin(theta1)
        x1, y1 = np.cos(theta2), np.sin(theta2)
        plt.scatter(x, y, s=50, c='violet', zorder=3)
        plt.plot([x,x1], [y,y1], color = 'w')

    circle = Circle((0, 0), 1, color='c', fill=False, lw = 1)
    ax.add_artist(circle)

    #Customize the axes and gridlines:
    ax.grid(False)
    ax.spines['right'].set_visible(False)
    ax.spines['top'].set_visible(False)
    ax.spines['left'].set_visible(False)
    ax.spines['bottom'].set_visible(False)
    #TickMarks Customization:
    ax.set_xticks([])
    ax.set_yticks([])


anim = animation.FuncAnimation(fig, f, frames=100, interval=100, blit=False)

plt.show()
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712