2012-12-21 51 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.DataFrame([dict({p:0.0 for p in df.columns}), ]) 

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

系列可以用空字符串來命名。

回答

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