Matplotlib 线旋转或动画

sss*_*sss 1 matplotlib python-3.x

我创建了极坐标图并想模仿多普勒。这包括围绕圆进行 360 度扫描(极坐标图)。一旦扫描达到 360 度,就需要回到零并继续扫描。

如何设置动画或旋转这条线以不断扫过这个圆?我只想要一条线不断地扫过这个情节。

我已经看过几个不同的例子,但是没有一个能够创建这种旋转。

import numpy as np
import math
import matplotlib.pyplot as plt
import pylab
import time

r = 90  * (math.pi/180)
t = 50000
az = 90
el = 5

fig = pylab.figure(figsize = [5.0, 5.0])
ax = fig.gca(projection = 'polar')
fig.canvas.set_window_title('Doppler')
ax.plot(r, t, color ='b', marker = 'o', markersize = '3')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)

currTime = time.time()
prevTime = currTime - 1
deltaTime = currTime - prevTime

outer_border_width = 1

screen_width = 500
screen_height = 500

midpoint = [int(screen_width/2), int(screen_height/2)]
radius = (midpoint[0])
sweep_length = radius - outer_border_width

angle = 50
sweep_interval = 10
sweep_speed = sweep_interval

x = sweep_length * math.sin(angle) + int(screen_width/2)
y = sweep_length * math.cos(angle) + int(screen_height/2)

az = az + ((360.0 / sweep_interval ) * deltaTime)

line1 = (midpoint, [50000, 50000])
#line2 = (midpoint, [20000, 20000])

ax.plot(line1, color = 'b', linewidth = 1)

#Increase the angle by 0.05 radians
angle = angle - sweep_speed

#Reset the angle to 0
if angle > 2 * math.pi:
    angle = angle - 2 * math.pi

#ax.plot(line2, color = 'r', linewidth = 1)
#ax.lines.pop(0)

plt.show()
Run Code Online (Sandbox Code Playgroud)

下面是目前的样子,供参考: 在此输入图像描述

非常感谢!

Imp*_*est 6

我不太明白你的代码,但为了生成动画,你可以使用matplotlib.animation.FuncAnimation. 在这里,您将向更新函数提供一个角度数组,该函数设置每帧的线数据。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation

r = 90  * (np.pi/180)
t = 50000

fig = plt.figure()
ax = fig.gca(projection = 'polar')
fig.canvas.set_window_title('Doppler')
ax.plot(r, t, color ='b', marker = 'o', markersize = '3')
ax.set_theta_zero_location('N')
ax.set_theta_direction(-1)
ax.set_ylim(0,1.02*t)

line1, = ax.plot([0, 0],[0,t], color = 'b', linewidth = 1)

def update(angle):
    line1.set_data([angle, angle],[0,t])
    return line1,

frames = np.linspace(0,2*np.pi,120)

fig.canvas.draw()
ani = matplotlib.animation.FuncAnimation(fig, update, frames=frames, blit=True, interval=10)

plt.show()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述