python绘制动态极坐标图:
方法一:利用matplotlib.animation异步绘制
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as antt
li = [element for element in range(0, 330, 30)]
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection='polar') # 极坐标图绘制
def dtt(r):
plt.cla()
# np.linspace(np.pi / 4, np.pi / 4, 2, endpoint=False)
theta = [np.pi/2, -np.pi/2]
radii = [r, r]
# 哪个角度画,长度,扇形角度,从距离圆心0的地方开始画
bars = ax.bar(theta, radii, width=np.pi / 2, bottom=0.0)
plt.rgrids(np.arange(0, 22, 2), angle=0) # 绘制1-9,步长为1的圆环
plt.thetagrids(li)
for bar in bars:
bar.set_facecolor(plt.cm.jet(r/9.0))
bar.set_alpha(0.5) # 添加颜色
return r
def dong():
r = 1
dir = 0
while True:
if dir == 0:
r += 1;
if r >= 20:
r = 20
dir = 1
else:
r -= 1
if r <= 1:
r = 1
dir =0
yield r
if __name__ == "__main__":
gif = antt.FuncAnimation(fig, dtt, dong, interval=20)
plt.show()
方法二:利用matplotlib.pyplot.pause()
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as antt
import time
li = [element for element in range(0, 330, 30)]
fig = plt.figure(figsize=(5, 5))
ax = fig.add_subplot(111, projection='polar') # 极坐标图绘制
def dtt(r):
plt.cla()
# np.linspace(np.pi / 4, np.pi / 4, 2, endpoint=False)
theta = [np.pi/2, -np.pi/2]
radii = [r, r]
# 哪个角度画,长度,扇形角度,从距离圆心0的地方开始画
bars = ax.bar(theta, radii, width=np.pi / 2, bottom=0.0)
plt.rgrids(np.arange(0, 22, 2), angle=0) # 绘制1-9,步长为1的圆环
plt.thetagrids(li)
for bar in bars:
bar.set_facecolor(plt.cm.jet(r/9.0))
bar.set_alpha(0.5) # 添加颜色
return r
if __name__ == "__main__":
lasttime = 0
r = 1
dir = 0
while True:
if time.time() * 1000 - lasttime > 20:
lasttime = time.time()*1000
if dir == 0:
r += 1
if r >= 20:
r = 20
dir = 1
else:
r -= 1
if r <= 1:
r = 1
dir = 0
dtt(r)
plt.pause(0.01)
方法三:
ax.relim()
ax.autoscale_view()
fig.canvas.draw()
fig.canvas.flush_events()
来源:https://blog.csdn.net/for_dream1205/article/details/98953843