Matplotlib:具有大误差条的循环角度图

这是我的问题:

我有一些物理数据表示-90至90度之间的角度。存在与此数据相关的已知错误。我正在使用numpy和matplotlib在python3中工作。

_我想为每个测量绘制带有误差线的数据。角度范围从-90到90,并且误差不应超出这些范围。例如,对于85 +/- 10度的角度,我希望上方的误差线循环回到-85,而不是回到95。

_有可能吗?怎么样?我正在尝试使用$ plt.fill_between()$或$ plt.errorbar()$,但是它不起作用。在上面的示例中,即使我尝试将错误栏强制设为-85,该错误也不会循环显示90 ...

以下是一些示例:

import matplotlib.pyplot as plt
import numpy as np

t = np.arange(10) #time
a = np.linspace(50,89,10) #fake angle value
e = np.array([10]*10) #error value
a_up  = a + e #Upper error bars
a_low = a - e #Lower error bars

f,ax = plt.subplots(nrows=2,ncols=2)

###Simple error graph,I don't want it because error bars outside of [-90,90]
ax[0,0].errorbar(t,a,yerr = e) #Plot the errors as error bars

### Same but with shaded area
ax[0,1].fill_between(t,a_low,a_up) #Plot the errors as filled region
ax[0,1].plot(t,"*r")

###My best option right now,put an upper limit everywhere
for i,u in enumerate(a_up):
    if u > 90:
        a_up[i] = 90

ax[1,0].fill_between(t,a_up) #Plot the errors
ax[1,0].plot(t,"*r")

###Finally,force all errorbars in [-90,90] (Just for this exemple,it's generalized in my code)
for i,u in enumerate(a_up):
    if u >= 90:
        a_up[i] -= 90

ax[1,"*r")

plt.show()


我希望我已经足够清楚,我无法在网络上找到解决方案...也许我不知道该如何制定。

预先感谢您的帮助,在使用您的答案10年之后,我终于有机会提出一个问题! :)

莱奥

zt48475144 回答:Matplotlib:具有大误差条的循环角度图

从David的回答中,我做了一些更改,以使其更加笼统:

import matplotlib.pyplot as plt

angles = [30,40,50,-60,10,85,-85,72,2,35]
errors = [2,4,30,12,4]

x = [i for i,j in enumerate(angles)]


ls = dict()

for i,error,angle in zip(x,errors,angles):
    if angle > 0 and angle + error > 90:
        temp = angle + error - 180
        ls.update({i:[-90,temp]})
    if angle < 0 and angle - error < -90:
        temp = angle - error + 180
        ls.update({i:[90,temp]})


plt.figure()
plt.ylim(-90,90)
plt.errorbar(x,angles,yerr=errors,fmt='C0 ',marker='o')
# plt.errorbar(list(ls.keys()),[-90,90],yerr=list(ls.values()),fmt='C0 ')
for i,a in ls.items():
    plt.vlines(i,a[0],a[1],colors='C0')

plt.ylabel('Angle')
plt.xlabel('Time (s)')

plt.show()

它可能未进行优化,但对我来说效果很好:) 再坦克!

,

没有简单的方法可以做到这一点:

import matplotlib.pyplot as plt

angles = [30,20,angles):

    if angle > 0 and abs(angle) + error > 90:
        temp = angle + error - 90
        ls.update({i:temp})

    if angle < 0 and abs(angle) + error > 90:
        temp = abs(angle) + error - 90
        ls.update({i:temp})


plt.figure()
plt.ylim(-90,fmt='C0 ')
plt.vlines(list(ls.keys())[0],list(ls.values())[0] - 90,-90,colors='C0')
plt.vlines(list(ls.keys())[1],90 - list(ls.values())[1],90,colors='C0')
plt.ylabel('Angle')
plt.xlabel('Time (s)')

plt.show()

enter image description here

该循环将查找误差超过90度限制的那些角度。这样,您有两个选择:

# plt.errorbar(list(ls.keys()),fmt='C0 ')

plt.vlines(list(ls.keys())[0],colors='C0')

这意味着您必须手动添加垂直线,可以编写一个函数来执行此操作,但这是第一次尝试。顺便说一句,您可能会看到,可视化效果不好。

本文链接:https://www.f2er.com/2716568.html

大家都在问