2017-02-11 63 views
1

我刚刚开始使用matplotlib和seaborn来绘制我的图。这是我写到目前为止使用seaborn调整子图块

count = 1 
l=[13,0,47,29,10] 
plt.figure(figsize=(30,40)) 

for ww in l: 
    temp_dict = defaultdict(list) 
    entropies = list() 
    for k,v in df.ix[ww].iteritems(): 
     e = 0 
     for i in v: 
      temp_dict[k].append(float(i)) 
      if not float(i) == 0: 
       e += -1.0*float(i)*log10(float(i)) 
     entropies.append(e) 

    y = entropies 
    x=(range(len(entropies))) 
    slope, intercept, r_value, p_value, std_err = stats.linregress(x,y) 

    plt.subplot(len(lista_autori),2,count) 
    tdf = pd.DataFrame.from_dict(temp_dict, orient='columns') 
    a = tdf.dropna() 
    sns.set(font_scale=2) 
    #sns.factorplot(size=2, aspect=1) 
    sns.heatmap(a,linewidths=.5,cmap="RdBu_r") 
    plt.xlabel(ur'year') 
    plt.ylabel(ur'$PACS$') 
    count +=1 

    plt.subplot(len(lista_autori),2,count) 
    plt.plot(x,y) 
    x1 = (range(28)) 
    y1 = [slope*i + intercept for i in x1] 
    plt.plot(x1,y1) 
    count +=1 


plt.tight_layout(); 

的代码,结果如下:

enter image description here

我想调整的每一行,指定行的2/3左手方的图片,剩下的就在右边。我试图看看给出的答案here,但是当我必须混合ax和seaborn的热图时,我发现了一些困难。任何帮助或其他解决方案?

回答

1

linked question的回答直接告诉您如何使用GridSpec实现不等列宽的网格。在这里我看不出太多的区别,但是下面对你来说可能更容易理解,因为它使用了seaborn热点图,不止一列,也没有“斧头”。

import numpy as np 
import matplotlib.pyplot as plt 
from matplotlib import gridspec 
import seaborn as sns 

# generate some data 
x = np.arange(0, 10, 0.2) 
y = np.sin(x) 
X = np.random.rand(3,4) 

fig = plt.figure(figsize=(8, 6)) 
gs = gridspec.GridSpec(5, 2, width_ratios=[2, 1]) 

for i in range(5): 
    plt.subplot(gs[i*2+0]) 
    sns.heatmap(X) 
    plt.subplot(gs[i*2+1]) 
    plt.plot(x,y) 

plt.show()