I have this task: create a heart animation using polar coordinates. Start from drawing a heart and then add animation as the next step; use variable increase of the rendering angle. Then i need to build other figures in polar coordinates the same way. I also need to make the drawing function separate from the figure, so that it would be possible to change figure with minimal code changes
I tried running this, put the output looks really weird and the other figures don't look as expected when i change theta and r. I am not sure if it's possible to complete this task using tkinter or pygame or something like that. I would be happy if you could come up with any ideas to complete this.
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
theta = np.linspace(0, 2*np.pi, 100)
r = 1 - (np.abs(theta) - 1) ** 2
line, = ax.plot([], [], color='red')
def init():
line.set_data([], [])
return line,
def animate(i):
current_theta = theta[:i]
current_r = r[:i]
x_polar = current_r * np.cos(current_theta)
y_polar = current_r * np.sin(current_theta)
line.set_data(x_polar, y_polar)
return line,
ani = animation.FuncAnimation(fig, animate, init_func=init, frames=len(theta), interval=50, blit=True)
plt.show()



It seems you are using the wrong expression for the radius. Your radius is proportional to the sqare of theta and becoming very negative with increasing theta which cannot be represented in a polar plot. Most likely you are missing sine or cosine. Playing around a bit I found
r = 1 - (np.abs(np.cos(theta)) - 1) ** 2to produce a heart shape.In general though it seems odd to me to calculate theta and r, which are the coordinates of a polar plot and then doing something like a trasformation to cartesian coordinates (x_polar and y_polar) for your plot.
About the separating issue, move the calculation of the radius out of the animate function. The animate function should be responsible only for updating the plot:
I added a function for the Cardioid on Wikipedia, the 'heart curve', as an example for having several functions prepared.