2016-11-22 1 views
2

오류 막대 (막대 상단 주위에 대칭 됨)가 포함 된 판다 막대 그래프로 데이터를 플로팅하고,이 플롯에서 하나의 오류 막대 정도를 수정하고 싶습니다. 그것의 절반 만 보여줍니다. 어떻게해야합니까?pandas barplot의 오류 막대 범위 수정하기

barplot example with errorbars

내가 에 사후 접근을 시도하고의 errorbar의 범위를 수정하고 있습니다 : 그런 음모를 산출

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 

bars = pd.DataFrame(np.random.randn(2,2), index=['a','b'], columns=['c','d']) 
errs = pd.DataFrame(np.random.randn(2,2), index=['a','b'], columns=['c','d']) 

ax = bars.plot.barh(color=['r','g'],xerr=errs) 

: 여기

은 구체적인 예이다 인덱스 a 및 열 d이 표시되므로 첫 번째 절반 만 표시됩니다. 즉 [bar_top-err, bar_top+err] 대신 [bar_top-err, bar_top] 세그먼트 . 나는 다음하기 matplotlib 개체를 검색하려고 :

내가 잘못 아니에요 경우 Bbox이며, 올바른 목적을 설명합니다,하지만 난 내 음모에 수정을 얻을 수 없다,
plt.getp(ax.get_children()[1],'paths')[0] 

. 어떻게하는지에 대한 아이디어가 있습니까?

+0

'유형 (plt.getp (도끼. get_children() [1], 'paths') [0])'실제로'matplotlib.path.Path'라고 알려줍니다. – whrrgarbl

+0

물론'plt.getp (ax.get_children() [1], 'paths') [0] .get_extents()'는 matplotlib.transforms.Bbox' 타입입니다. 그러나 구체적으로,이 솔루션을 알아내는 데 도움이되지 않습니다 ... – fseyrl

답변

1

거의 다 있었으므로 좌표를 수정하고 업데이트해야합니다. path.vertices. 난 그냥 그것의 부정적인 부분을 보여주는 대신, 오류 표시 줄이 "0에서 먼"에 직면 할 것으로 가정하는 자유를했다 :

import numpy as np 
import matplotlib.pyplot as plt 
import pandas as pd 

bars = pd.DataFrame(np.random.randn(2,2), index=['a','b'], columns=['c','d']) 
errs = pd.DataFrame(np.random.randn(2,2), index=['a','b'], columns=['c','d']) 

ax = bars.plot.barh(color=['r','g'], xerr=errs) 
child = ax.get_children()[1] 

path = plt.getp(child, 'paths')[0] 
bar_top = path.vertices.mean(axis=0)[0] 

# replace the right tail if bar is negative or left tail if it's positive 
method = np.argmin if np.sign(bar_top)==1 else np.argmax 
idx = method(path.vertices, axis=0)[0] 
path.vertices[idx, 0] = bar_top 

plt.savefig('figs/hack-linecollections.png', dpi=150) 
plt.show() 

hack-linecollections

+0

위대한, 감사합니다! 그게 내 문제를 정확하게 해결해줍니다. – fseyrl

관련 문제