【问题标题】:Seaborn displaying one barplot instead of multiple plotsSeaborn 显示一个条形图而不是多个图
【发布时间】:2017-03-16 08:03:03
【问题描述】:

我的代码看起来像

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,其值为 0.1 和 0.2。然后,左下角或右上角的图应显示 CD,其值为 0.3 和 0.4。以此类推

然而,我得到的是:

我该如何解决这个问题?

【问题讨论】:

    标签: python seaborn


    【解决方案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() 移到循环之外。

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 1970-01-01
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 2020-01-02
      • 2017-10-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多