2017-03-15 52 views
1

我的代码看起来像Seaborn显示一个barplot不是多个地块

ylst = [.1,.2,.3,.4,.5,.6,.7,.8] 
xlst=["A","B","C","D","E","F","G","H"] 
with PdfPages('probs.pdf') as pdf_pages: 
    for i in range(0,len(xlst), 2): 
     ax=sns.barplot(xlst[i:i+2], ylst[i:i+2]) 
     ax.axes.set_title('Which name', fontsize=24,color="b",alpha=0.3) 
     ax.set_ylabel("Probability (%)",size = 17,color="r",alpha=0.5) 

     for p in ax.patches: 
      ax.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005)) 
     pdf_pages.savefig() 

我想4个地块,在2行2列的矩阵。也就是说,左上角的图应该显示AB,值为.1和.2。然后,左下角或右上角的图应显示值为.3和.4的CD。等

不过,我反而得到的是:

enter image description here

我该如何解决这个问题?

回答

1

我测试过这个工作的:

import matplotlib.pyplot as plt, seaborn as sns 
from matplotlib.backends.backend_pdf import PdfPages 

ylst = [.1,.2,.3,.4,.5,.6,.7,.8] 
xlst=["A","B","C","D","E","F","G","H"] 
with PdfPages('probs.pdf') as pdf_pages: 
    fig,((ax1,ax2),(ax3,ax4)) = plt.subplots(2,2) 
    list_of_axes = (ax1,ax2,ax3,ax4) 
    for i,j in zip(range(0,len(xlst), 2),list_of_axes): 
     sns.barplot(xlst[i:i+2], ylst[i:i+2],ax=j) 
     j.axes.set_title('Which name', fontsize=24,color="b",alpha=0.3) 
     j.set_ylabel("Probability (%)",size = 17,color="r",alpha=0.5) 

     for p in j.patches: 
      j.annotate(str(p.get_height()), (p.get_x() * 1.005, p.get_height() * 1.005)) 
    pdf_pages.savefig() 
    plt.close() 

我刚刚分配轴为每个插曲和移动.savefig()外循环。