2017-01-03 52 views
3

我想绘制3个水平条形图,标签为y轴,数据为x轴,我希望每个图都是不同的颜色,并有一定类型的注释,如依赖于由列中的数据表示的signficiance,例如星号:如何使用seaborn pairgird和seaborn barplot为每个图添加颜色和星号

dat = pd.DataFrame({ 
    'Labels':['v1','v2','c1','c2'], 
    'Ave': [.2, .3, .5, .9], 
    'SD': [0.02, 0.1, 0.04, 0.06], 
    'Tot': [3, 4, 6, 8], 
    'Sig': [0.05, 0.001, 0.0001, 0.05] 
}) 

sns.set_style('white') 

g = sns.PairGrid(dat, x_vars=['Ave', 'SD', 'Tot'], y_vars=['Labels']) 
g.map(sns.barplot) 

会让我这样的事情:

enter image description here

我如何获得每个小区的“Ave”“SD”和“ToT “是他们自己的颜色?以及如何添加注释来表示“Sig”列给出的重要性?

回答

0

还没有想出如何得到这个与seaborn做,但这个matplotlib将工作

import pandas as pd 

dat = pd.DataFrame({ 
    'Labels':['v1','v2','c1','c2'], 
    'Ave': [.2, .3, .5, .9], 
    'SD': [0.02, 0.1, 0.04, 0.06], 
    'Tot': [3, 4, 6, 8], 
    'Sig':[0.05, 0.005, 0.0001, 0.05], 
    'Sig_mask': [1,2,3,1], 
}) 

import matplotlib.pyplot as plt 
fig = plt.figure() 

# multiple plots 
ax1 = fig.add_subplot(131) 

# horizontal bar plot and set color 
ax1.barh(dat.index.values, dat['SD'], color='r', align='center') 

# SET SPINES VISIBLE 
ax1.spines['right'].set_visible(False) 
ax1.spines['top'].set_visible(False) 

# SET X, Y LABELS 
ax1.set(yticks=dat.index.values, yticklabels=dat.Labels.values) 
ax1.set(xlabel='SD') 

# ADD SIG NOTATION 
for idx, val in enumerate(dat.SD.values): 
    ax1.text(x=val+.003, y=idx, s='*'*dat.Sig_mask[idx], va='center') 

# ADD SECOND PLOT 
ax2 = fig.add_subplot(132) 
# everything else much in the same manner, change color and data column 


# ADD THIRD PLOT 
ax3 = fig.add_subplot(133) 
# same as above 
1

你可以通过你的数据整形为长(整齐)格式的亲近和使用factorplot

重塑:

dat = (
    pandas.DataFrame({ 
     'Labels':['v1','v2','c1','c2'], 
     'Ave': [.2, .3, .5, .9], 
     'SD': [0.02, 0.1, 0.04, 0.06], 
     'Tot': [3, 4, 6, 8], 
     'Sig': [0.05, 0.001, 0.0001, 0.05] 
    }).set_index('Labels') 
    .unstack() 
    .reset_index() 
    .rename(columns={ 
     'level_0': 'stat', 
     0: 'result' 
    }) 
) 

print(dat.head(8)) 

    stat Labels result 
0 Ave  v1 0.20 
1 Ave  v2 0.30 
2 Ave  c1 0.50 
3 Ave  c2 0.90 
4 SD  v1 0.02 
5 SD  v2 0.10 
6 SD  c1 0.04 
7 SD  c2 0.06 

而且factorplot:

seaborn.factorplot('result', 'Labels', data=dat, 
        kind='bar', sharex=False, 
        hue='stat', hue_order=stats, 
        col='stat', col_order=stats) 

enter image description here

的问题是条偏移因为使用hue告诉seaborn在每个Y,以腾出空间为不同类别-位置。

在下一个版本中,您将能够说出dodge=False以避免这种偏移。