python - Matplotlib - 在网格中放置饼图

标签 python matplotlib charts pie-chart

我试图在网格中组织饼图,在左侧的另一列中,我在特定行的饼图上写了一些相关数据。现在我的代码是:

data = [[0.3,0.3,0.3,0.1],[0.2,0.2,0.2,0.4],[0.1,0.1,0.4,0.4]]


def main():
    createPieCharts(data)
    return 0


def createPieCharts(data,piecharts_fname="piecharts"):
    """ """
    import datetime
    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    areas = ['Arava','Lotz','Paran','Ramon']
    thresholds = [0.0,0.01,0.02]
    num_columns = len(areas)
    num_rows    = len(thresholds)
    with PdfPages(piecharts_fname+'.pdf') as pdf:
        fig, ax = plt.subplots(num_rows,num_columns)
#            fig.suptitle('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
        labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
        for i,threshold in enumerate(thresholds):
            fracs = data[i]
            for j,area in enumerate(areas):
                ax[i,j].set_title(area)
                ax[i,j].pie(fracs, labels=labels,
                            autopct='%1.1f%%', shadow=True, startangle=90)
                ax[i,j].set_aspect('equal')
        plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
        pdf.savefig(fig)
        plt.close()
        # Adding metadata for PDF file
        d = pdf.infodict()
        d['Title'] = 'Pie charts'
        d['CreationDate'] = datetime.datetime.today()

if __name__ == '__main__':
    main()

结果是这样的 enter image description here 虽然我希望它像本例一样在左侧有另一列 - enter image description here

聪明的做法是什么?

最佳答案

添加一个额外的列,并将您的文本放在那里。

然后您只需关闭左列 (ax.set_axis_off()) 中轴的轴线和刻度,然后使用 ax.text 添加文本标签。

请注意,对于绘制饼图的其他轴,您还需要将 j 索引加 1。

这是您脚本中的函数,已修改:

def createPieCharts(data,piecharts_fname="piecharts"):
    """ """
    import datetime
    from matplotlib.backends.backend_pdf import PdfPages
    import matplotlib.pyplot as plt
    areas = ['Arava','Lotz','Paran','Ramon']
    thresholds = [0.0,0.01,0.02]
    num_columns = len(areas) + 1                                # Add extra column for text
    num_rows    = len(thresholds)
    with PdfPages(piecharts_fname+'.pdf') as pdf:
        fig, ax = plt.subplots(num_rows,num_columns)
#            fig.suptitle('Raining Hogs and Dogs', bbox={'facecolor':'0.8', 'pad':5})
        labels = ['Frogs', 'Hogs', 'Dogs', 'Logs']
        for i,threshold in enumerate(thresholds):
            fracs = data[i]
            ax[i,0].set_axis_off()                                  # Turn off axes & ticks
            ax[i,0].text(0.5,0.5,threshold,ha='center',va='center') # Add text
            for j,area in enumerate(areas):
                ax[i,j+1].set_title(area)                           # Add 1 to j index
                ax[i,j+1].pie(fracs, labels=labels,                 # Add 1 to j index
                            autopct='%1.1f%%', shadow=True, startangle=90)
                ax[i,j+1].set_aspect('equal')                       # Add 1 to j index
        plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)
        pdf.savefig(fig)
        plt.close()
        # Adding metadata for PDF file
        d = pdf.infodict()
        d['Title'] = 'Pie charts'
        d['CreationDate'] = datetime.datetime.today()

enter image description here

关于python - Matplotlib - 在网格中放置饼图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37653943/

相关文章:

python - pliSTLib 无法读取 Safari 的 plist 文件

python - matplotlib 中的动画箭头

javascript - 格式化饼图的工具提示

python - 递归入门

python - 在解释器中重新加载(更新)模块文件

python - AWS Elasticsearch 服务角色权限

python - Matplotlib 图例,跨列而不是向下添加项目

python - Matplotlib 中的像素化动画

angular - ng2-charts 空初始数据集

laravel - 在 Laravel 5.1 中实现 Charts.js