2017-04-19 1 views
2

그래서 seaborn을 사용하여 barplot을 만들려고합니다.barplot x 축에서 데이터를 생성합니다. seaborn python

revels_data = pd.read_csv("testtt.txt") rd = revels_data 

ax = sns.barplot(x="Packet number", y="Contents", data=rd) plt.show() 

나는 각 줄의 내부 색상으로 구분됩니다 (x 축)에 각 패킷 번호에 대한 막대를 만들려고 해요 : 내 데이터는

Packet number,Flavour,Contents 
1,orange,4 
2,orange,3 
3,orange,2 
4,orange,4 
... 
36, orange,3 
1, coffee,5 
2, coffee,3 
... 
1, raisin,4 
etc. 

내 코드는 현재 형태입니다 y 축상의 패킷 당 총 내용이있는 맛.

는 각 패킷 즉의 합계를 만들려고 시작

total_1 = (rd.loc[rd["Packet number"] == 1, "Contents"].sum()) 

하지만 난 거기에서 가고 싶어 방법을 잘 모르는, 또는 그것을 할 수있는 쉬운 방법이 있다면.

많은 조언을드립니다.

답변

2

hue을 사용하고 싶습니다. 또한 현재 각 카테고리의 평균을 표시하고 있습니다. 다른 기능을 계산하려면 estimator을 사용할 수 있습니다.

ax = sns.barplot(x="Packet number", y="Contents", hue="Flavour", estimator=np.sum, data=rd) 

편집 :

만약 당신이 대신 평균의 합을 표시 할 경우

ax = sns.barplot(x="Packet number", y="Contents", hue="Flavour", data=rd) 

을 또는 :

따라서

, 코드가 있어야한다 누적 막대 그래프에 관심이 있으시면 팬더를 사용하여 직접 만들 수 있지만 n 귀하의 데이터를 먼저 그룹화하십시오 :

# Sum (or mean if you'd rather) the Contents per packet number and flavor 
# unstack() will turn the flavor into columns, and fillna will put 0 in 
# all missing columns 
grouped = rd.groupby(["Packet number", "Flavour"])["Contents"].sum().unstack().fillna(0) 

# The x axis is taken from the index. The y axis from the columns 
grouped.plot(kind="bar", stacked=True) 
+0

정말 고맙습니다. 어떻게 각 풍미에 대해 여러 개의 막대 대신 하나의 막대 안에 풍미 구분이있는 단일 막대가 있습니까? – mystifier

+0

나는 당신이 seaborn과 함께 할 수 있다고 생각하지 않는다. 그러나 '팬더'로 직접 할 수 있습니다. 게시물에 추가하겠습니다. – tmrlvi

+0

brilliant, thanks – mystifier