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.

10
  • Possible duplicate of How to animate a scatter plot? Commented Sep 22, 2018 at 10:17
  • Another link here Commented Sep 22, 2018 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.. Commented Sep 22, 2018 at 10:23
  • and I also prefer FuncAnimation method... Commented Sep 22, 2018 at 10:24
  • I try to do this using your links ...But it doesn't do my job... Commented Sep 22, 2018 at 10:43

1 Answer 1

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() 
Sign up to request clarification or add additional context in comments.

2 Comments

It works fine....but it plots f(n) for all values of n and keep them.....Is it possible to clear the screen after each call f(n)....?
You may clear the axes for each step. I updated the code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.