python 绘制的角度没有跳出范围 [0 360] [重复] 。

问题描述 投票:0回答:1

我想用python写一个脚本来解决以下问题。

当我绘制一些角度轨迹时,由于我跟踪的点有时会在[0, 360]范围外 "跳跃",所以我得到了跳跃。考虑到这些跳跃很容易识别(参见 plot 中的链接),我想在python中写一个函数,将角度列表作为输入,当它识别出了 jumps >300 它将从列表的以下值中减去360,直到它找到第二个跳转.这是我试图编写代码,但不幸的是,我正在运行一个无限循环。有人有什么建议吗?

EDIT我添加了一个列表,其中包括一些值,只是作为一个例子。

alpha = [356.37, 359.80, 357.14, 359.18, 350.97, 347.35, 348.98, 351.80,  2.74, 354.55, 354.13, 357.82,  3.86, 2.42, 3.57, 1.57, 357, 358]


#take derivative of list of angles alpha
alpha_diff = [0, np.diff(alpha)]
#iterate over each item in alpha_diff and when it finds jumps <-360 you will add 360 until it finds the next jump
for i in alpha_diff:
    if alpha_diff[i]<-300:
        while alpha_diff[i]<100:
            alpha[i] = alpha[i]+360
#similarly if finds jumps > 360 you will subtract 360 until it finds the next jump
    if alpha_diff[i]>300:
        while alpha_diff[i]<100:
            alpha[i] = alpha[i]-360 
    else:
        alpha[i] = alpha

enter image description here

python while-loop range angle
1个回答
1
投票

好吧,你正在寻找 unwrap. 在你需要一个度数到辐射的转换。这里有一个例子,使用你的数据。

import matplotlib.pyplot as plt
import numpy as np
from math import pi

alpha = [356.37, 359.80, 357.14, 359.18, 350.97, 347.35, 348.98, 351.80,  2.74, 354.55, 354.13, 357.82,  3.86, 2.42, 3.57, 1.57, 357, 358]
alpha_rad = [x*pi/180 for x in alpha]
alpha_unwrap= np.unwrap(alpha_rad)

然后,如果你 plt.plot(alpha) 你获得。

enter image description here

plt.plot(alpha_rad)

enter image description here

最后,当 plt.plot(alpha_unwrap):

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.