I am trying to create a marine-style compass (fixed needle, rotating rose) using the polar plot of matplotlib.
# compass animated with matplotlib
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
fig = plt.figure()
ax = fig.add_subplot(111, polar=True)
ax.set_rmax(1)
ax.set_rticks([0.5, 1])
ax.set_theta_direction(-1)
ax.set_xticks(np.arange(np.radians(0), np.radians(360), np.radians(45)))
ax.set_xticklabels(np.arange(0,360,45), fontsize='small')
line, = ax.plot([], [], lw=2)
ax.grid(True)
angle = 0
def init():
line.set_data([], [])
return line,
def animate(i):
global angle
angle += 0.1
x = [0, np.cos(angle)]
y = [0, np.sin(angle)]
line.set_data([np.cos(angle),np.cos(angle)], [0, 1])
ax.set_theta_offset(np.cos(angle)+np.pi/2)
return [line]
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=100, interval=20, blit=False)
plt.show()
yields this:
as you can see I am using blit=False to make this work. But this also means it does not run smoothly together with the other plots.
Is there a way to make this work with blitting?
