2012-12-21 2 views
3

이것은 거의 작동하는 해킹입니다.팬더에서 막대 차트 클러스터를 시각적으로 분리

#!/usr/bin/env python 

from pandas import * 
import matplotlib.pyplot as plt 
from numpy import zeros 

# Create original dataframe 
df = DataFrame(np.random.rand(5,4), index=['art','mcf','mesa','perl','gcc'], 
         columns=['pol1','pol2','pol3','pol4']) 
# Estimate average 
average = df.mean() 
average.name = 'average' 

# Append dummy row with zeros and then average 
row = DataFrame([dict({p:0.0 for p in df.columns}), ]) 

df = df.append(row) 
df = df.append(average) 

print df 

df.plot(kind='bar') 
plt.show() 

및 범 :

   pol1  pol2  pol3  pol4 
art  0.247309 0.139797 0.673009 0.265708 
mcf  0.951582 0.319486 0.447658 0.259821 
mesa  0.888686 0.177007 0.845190 0.946728 
perl  0.902977 0.863369 0.194451 0.698102 
gcc  0.836407 0.700306 0.739659 0.265613 
0  0.000000 0.000000 0.000000 0.000000 
average 0.765392 0.439993 0.579993 0.487194 

enter image description here

그것은 기준과 평균 사이의 시각 거리를 제공한다. x 축에서 0을 없애는 방법이 있습니까 ??


DataFrame에서 이런 식으로 더미 행을 사용할 수 없다는 사실이 밝혀졌습니다. 내 솔루션은 빈 문자열로 이름을 지정할 수 있습니다

row = pd.Series([dict({p:0.0 for p in df.columns}), ]) 
row.name = "" 

시리즈에

row = pd.DataFrame([dict({p:0.0 for p in df.columns}), ]) 

을 변경했다.

답변

3
여전히

꽤 해키,하지만 작동합니다

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

# Create original dataframe 
df = pd.DataFrame(np.random.rand(5,4), index=['art','mcf','mesa','perl','gcc'], 
         columns=['pol1','pol2','pol3','pol4']) 
# Estimate average 
average = df.mean() 
average.name = 'average' 

# Append dummy row with zeros and then average 
row = pd.DataFrame([dict({p:0.0 for p in df.columns}), ]) 

df = df.append(row) 
df = df.reindex(np.where(df.index, df.index, '')) 
df = df.append(average) 
print df 

df.plot(kind='bar') 
plt.show() 

enter image description here