2011-08-28 3 views
2

matplotlib에 오차 막대가있는 극 막대 막대를 작성하려고합니다. 다음 코드를 사용할 때 모든 오류 막대는 가로 맞춤을 가지며 막대가 90 또는 270 도의 경우에 해당하지 않는 한 잘못 표시됩니다.극좌표 플롯 오차 막대가 matplotlib에서 각도와 함께 회전하지 않습니다.

from numpy import * 
from matplotlib import pyplot as py 


r=zeros([16]) 
err=zeros([16]) 
for i in range(16): 
    r[i]=random.randint(400,600) 
    err[i]=random.randint(20,50) 
theta=arange(0,2*pi,2*pi/16) 
width = pi*2/16 

fig = py.figure(figsize=(8,8)) 
ax = fig.add_axes([0.1, 0.1, 0.75, 0.79], polar=True) 

bars = ax.bar(theta+pi/16, r, width=width, bottom=0.0,yerr=err) 
ax.set_ylim(0,700) 
py.show() 

the figure

어떻게 오차 막대가 계정에 각 줄의 세타를 취할 얻으려면?

답변

2

그래서 오류 막대는 Line2D 객체로 생성 된 것처럼 보입니다. 즉 점선은 에러 막대 위치 (x [i], y [i] + yerr [i])에 해당하는 데이터 포인트로 플롯됩니다. 줄에있는 대시는 항상 기호이기 때문에 항상 동일합니다. 극지 음모에서는 분명히 작동하지 않습니다. 따라서이 오류 막대 설정을 삭제하고 각 오류 막대를 개별적으로 추가해야하며 올바른 방향의 선이 있어야합니다.

from matplotlib.lines import Line2D 
from math import acos,sqrt 

def correct_errorbar(ax,barlen=50,errorline=1): 
    """ 
    A routine to remove default y-error bars on a bar-based pie chart and 
    replace them with custom error bars that rotate with the pie chart. 
    ax -- the axes object that contains the polar coordinate bar chart 
    barlen -- the perpendicular length of each error bar 
    errorline -- the number of the Line2D object that represents the original 
     horizontal error bars. 

    barlen will depend on the magnitude of the "y" values, ie the radius. 
    This routine was tested with a plot consisting solely of bar chart, and 
    if other Line2D objects are added, either directly or through further 
    plot commands, errorline many need to be adjusted from its default value. 
    """ 
    # get error bar positions 
    x,y = ax.lines[errorline].get_data() 
    # remove incorrect bars 
    del ax.lines[errorline] 
    # add new lines fixing bars 
    for i in range(len(y)): 
     r = sqrt(barlen*barlen/4+y[i]*y[i]) 
     dt = acos((y[i])/(r)) 
     newline = Line2D([x[i]-dt,x[i]+dt],[r,r],lw=barlen/100.) 
     ax.add_line(newline) 
: 여기

이 작업을 수행하는 루틴입니다
관련 문제